我不知道一年后我是如何遇到这个问题的,但由于这个问题没有答案。。。
您可以使用Paypal url作为表单的操作,而不是自行提交表单然后重定向到Paypal,但在表单提交到Paypal之前,可以使用Ajax保存表单数据。您还可以使用Ajax响应在提交前准备表单,例如填写一些隐藏字段
下面是一个示例:
我的表单。php:
<form id="myform" method="post" action="<?php echo $paypal_url; ?>">
<input type="text" ...>
<input type="hidden" name="my-hidden-field" id="my-hidden-field" value="this is hidden">
...other form fields...
</form>
保存表单。php:处理Ajax请求并返回响应,该响应可用于在提交前操纵表单
<?php
if (!empty($_POST)) {
//save $_POST data to the database
...
//also, fill some data as response
$response = array(\'my_hidden_field\' => \'this is now filled in\');
//next line returns the response
echo json_encode($response);
}
我的形式。js:将表单数据提交到saveform。提交至贝宝前的php
$j = jQuery.noConflict();
$j(document).ready(function() {
$j(\'#myform\').submit(submit_myform);
});
function submit_myform() {
if (!myform_is_valid()) {
window.location.href = "#myform";
return false;//prevent normal browser submission, to display validation errors
} else {
$j.ajax({
url: \'saveform.php\',
type: \'POST\',
data: $j(this).serialize(),
dataType:\'json\', //data type of Ajax response expected from server
success: myform_success //callback to handle Ajax response and submit to Paypal
});
return false;//prevent normal browser submission, since the form is submitted in the callback
}
}
function myform_is_valid() {
$valid = true;
//validate data; if there\'s a validation error, set $valid=false and display errors on page
...
return $valid;
}
function myform_success(response) {
//this is called whenever the ajax request returns a "200 Ok" http header
//manipulate the form as you wish
$j(\'#my-hidden-field\').val(response.my_hidden_field);
//submit the form (to the form\'s action attribute, i.e $paypal_url)
document.forms[\'myform\'].submit();
}
而不是jquery。ajax调用您也可以使用较短的:
$j.post(\'saveform.php\',$j(\'#myform\').serialize(),myform_success,"json")