【问题标题】:PayPal Button submitting a form and triggering an Ajax call at the same time?PayPal Button 提交表单并同时触发 Ajax 调用?
【发布时间】:2013-03-03 05:58:22
【问题描述】:

我有一个 PayPal 按钮,可以将表单提交到表单的 action="" 属性中指定的 url。我希望此按钮执行的操作是在提交表单的同时(或大致相同)触发 ajax 调用。

所以表格看起来像这样:

<?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>
          ');
?>

ajax 请求如下:

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

    }
});

【问题讨论】:

    标签: php ajax


    【解决方案1】:

    这里的问题是,您的 ajax 请求将被取消,因为页面将重新加载(实际上重定向到 paypal)。

    您可以做的是在提交表单时发送 ajax 请求并等待直到 ajax 请求完成。然后,发送表格。

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

    使用onsubmit,当用户发送表单时,函数ajaxRequest 将被触发。由于我们之前放了一个return,所以form会等待函数的return继续。

    • 如果ajaxRequest()返回true,将提交表单。
    • 如果返回 false,则不会。

    这是 POC。您有更好的方法以非阻碍性的方式处理它(例如,在表单 id 上使用 jQuery)。而且您还必须编写函数代码。

    【讨论】:

      猜你喜欢
      • 2012-03-29
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 1970-01-01
      • 2012-07-29
      • 2021-09-13
      相关资源
      最近更新 更多