gpt4 book ai didi

php - form action 和 post 方法如何一起工作?

转载 作者:行者123 更新时间:2023-11-29 15:26:26 26 4
gpt4 key购买 nike

下面是我的代码。如果我不在表单 action="https://www.paypal.com/cgi-bin/webscr"中使用此链接。数据的形式成功插入到我的数据库中。但是,当我使用此操作链接时。在数据库中,表单数据未发送

那么,form action 和 post 方法如何一起工作?

实际上,这是我想要的,单击提交按钮后,将成功地将标题数据插入到我的数据库中。然后页面重定向到 PayPal 结账页面。

<?php
$database_username = 'root';
$database_password = '';
$pdo_conn = new PDO( 'mysql:host=localhost;dbname=shipping_pro', $database_username, $database_password );

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['insert'])) {
$sql = "INSERT INTO posts (title) VALUES (:title)";
$pdo_statement = $pdo_conn->prepare( $sql );
$result = $pdo_statement->execute( array(
':title'=>$_POST['title']
) );
if (!empty($result) ){
header('location:index.php');
}
}
?>
<form method="POST" action="https://www.paypal.com/cgi-bin/webscr">
<input type="text" name="title" placeholder="Title" />
<input name="insert" type="submit" value="Add">
</form>

最佳答案

我刚刚快速测试了使用ajax并让回调提交表单我在上面的评论中提到的想法 - 似乎工作正常,所以很想知道为什么这对你不起作用- 从本质上讲,该表格对于 Paypal 的所有意图和目的来说都是如此,所以我不明白为什么它不起作用(但也许还没有足够的咖啡?)

通过阻止“提交”按钮在单击时执行默认操作 (e.preventDefault()),您可以中断表单流程,并可以在提交之前注入(inject)您自己的逻辑。您不是立即发送表单,而是将所需的任何数据发送到 PHP 脚本,然后它会记录到数据库并向 ajax 函数发送回复,说“嗨,我已经完成 - 继续”等,然后回调实际上会继续提交表格。

<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
ob_clean();

/* log details to db */

/*

$database_username = 'root';
$database_password = '';
$pdo_conn = new PDO( 'mysql:host=localhost;dbname=shipping_pro', $database_username, $database_password );

INSERT INTO posts (title) VALUES (:title) etc etc

*/

/* send some sort of response to ajax callback */
exit("banana's tend to be yellow and monkeys love 'em!");
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Paypal: Record details to db and then submit to Paypal</title>
<script>
const ajax=function(url,params,callback){
let xhr=new XMLHttpRequest();
xhr.onload=function(){
if( this.status==200 && this.readyState==4 )callback.call( this, this.response )
};
xhr.onerror=function(e){
alert(e)
};
xhr.open( 'POST', url, true );
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.setRequestHeader('X-Requested-With','XMLHttpRequest');
xhr.send( params );
};

const clickhandler=function(e){
e.preventDefault();

let form=this.parentNode;

const callback=function(r){
alert( 'Response from saving to db: '+r+'\nNow submit the form to PayPal!!!' );
form.submit();
};
ajax( location.href, 'title='+this.previousElementSibling.value, callback )
};
document.addEventListener('DOMContentLoaded', ()=>{
document.querySelector( 'form > input[type="submit"][name="insert"]' ).addEventListener( 'click', clickhandler )
});
</script>
</head>
<body>
<form method="POST" action="https://www.paypal.com/cgi-bin/webscr">
<input type='hidden' name='cmd' value='_s-xclick' />
<input type='hidden' name='hosted_button_id' value='S2QXVVPPL7EUE' />
<input type='hidden' name='on0' value='BANANA' />
<input type='hidden' name='os0' value='BANANA' />
<input type='hidden' name='currency_code' value='GBP' />

<input type="text" name="title" placeholder="Title" />
<input name="insert" type="submit" value="Add">
</form>
</body>
</html>

关于php - form action 和 post 方法如何一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59045573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com