doter1995 2013-03-14 18:02
浏览 93
已采纳

PayPal按钮同时提交表单并触发Ajax调用?

I have a PayPal button which submits a form to the url specified int the form's action="" attribute. What I would like this button to do is trigger an ajax call at the same time (or roughly the same) with submitting the form.

So the form looks like this:

<?php echo('   

    <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" id="payWithPayPalForm">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="me@mydomain.com">
    <input type="hidden" name="amount" value="'.$price.'">
    <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest" >
    <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" id="payWithPayPalButton" name="submit" alt="PayPal - The safer, easier way to pay online!">
    <img alt="" border="0" src="https://www.paypalobjects.com/pl_PL/i/scr/pixel.gif" width="1" height="1">
    </form>
          ');
?>

And the ajax request is following:

$.ajax({
    type: "POST",
    url: "classes/acceptOfferFunction.php",
    data: {
        hash: getURLParameter('hash'),
        hash2: getURLParameter('hash2')
    },
    success: function (result) {

    }
});
  • 写回答

1条回答 默认 最新

  • doutou7286 2013-03-14 18:08
    关注

    The problem here, is that your ajax request will be cancelled because the page will reloaded (in fact redirected to paypal).

    What you can do, is to send the ajax request when your submit the form and wait until the ajax request finish. Then, send the form.

    <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" onsubmit="return ajaxRequest()" method="post" id="payWithPayPalForm">
    

    Using onsubmit, when user will send the form, the function ajaxRequest will be fired. Since we put a return before, form will wait for the return of the function to continue.

    • if ajaxRequest() return true, the form will be submitted.
    • if it returns false, it won't.

    This is POC. You have better way to handle that in a non-obstructive way (using jQuery on the form id for example). And you will have to code the function too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现