在PayPal按钮重定向之前完成AJAX调用 [英] Complete AJAX call before PayPal button redirects
问题描述
我正在使用PayPal按钮,并希望在按钮将用户重定向到PayPal之前,通过AJAX调用(或其他任何可行的方法)将记录插入数据库.
I am using PayPal buttons and would like to insert a record into the database via an AJAX call (or any other method that works) before the button redirects the user to PayPal.
我看过类似的问题/答案,但无法使它们起作用.他们要么重定向到PayPal,要么插入数据库记录……但不能两者都选.
I have looked at similar questions/answers but can't get them to work. They either redirect to PayPal or insert a database record...but not both.
主要问题:
在paypal之前提交表单值, PayPal按钮提交一个表单并同时触发Ajax调用?,在提交常规表单之前请求AJAX
Similiar questions:
Submit form values before paypal, PayPal Button submitting a form and triggering an Ajax call at the same time?, AJAX request before regular form is being submitted
这是我的表格:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top" name="paypalform" id="paypalform">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="XXXXXXXXXXXX">
<button type="submit" class="radius paypal-button" name="submit" alt="PayPal - The safer, easier way to pay online!">Buy Now!
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
这是我的javascript:
This is my javascript:
$('paypalform').submit(function(e) {
e.preventDefault();
e.returnValue = false;
var $form = $(this);
$.ajax({
url: 'ajax.create_sponsorship.php',
data: 'XXXXXX',
dataType: 'json',
context: $form,
complete: function() {
this.off('submit');
this.submit();
}
});
});
PHP文件ajax.create_sponsorship.php返回TRUE(如果插入成功)或FALSE.
The PHP file, ajax.create_sponsorship.php, return either TRUE (if insert was successful) or FALSE.
使用上面的代码时,浏览器成功重定向到PayPal,但未将记录插入数据库.
When using the code above the broswer successfully redirects to PayPal but does not insert record into database.
如果我将第一行更改为此:
If I change the first line to this:
$('#paypalform').submit(function(e) {
基本上,添加#-然后会插入数据库记录,但它不会重定向到PayPal.
Basically, adding the # - then the database record gets inserted but it doesn't redirect to PayPal.
推荐答案
如下更新代码并尝试:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top" name="paypalform" id="paypalform">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="XXXXXXXXXXXX">
<button type="submit" class="radius paypal-button prevented" name="submit" id="paypalsubmit" alt="PayPal - The safer, easier way to pay online!">Buy Now!
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
脚本如下:
<script>
$(document).ready(function(){
$('#paypalsubmit').click(function(e) {
if($(this).hasClass('prevented')){
e.preventDefault();
$(this).removeClass('prevented');
$.post( "ajax.create_sponsorship.php",{ data: 'XXXXXX'}, function( result ) {
if(result){
$('#paypalsubmit').click();
return true;
}
});
}else{
$('#paypalform').submit();
$(this).addClass('prevented');
}
});
})
</script>
希望这会有所帮助.
这篇关于在PayPal按钮重定向之前完成AJAX调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!