PayPal按钮提交表单并触发一个Ajax调用在同一时间? [英] PayPal Button submitting a form and triggering an Ajax call at the same time?

查看:447
本文介绍了PayPal按钮提交表单并触发一个Ajax调用在同一时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PayPal按钮,提交表单的int形式的行动=属性指定的URL。我想这个按钮做的是触发的同时Ajax调用(或大致相同)与提交表单。

所以,形式是这样的:

 < PHP的echo('

    <形式的行动=htt​​ps://www.sandbox.paypal.com/cgi-bin/webscr方法=后ID =payWithPayPalForm>
    <输入类型=隐藏名称=CMD值=_ xclick>
    <输入类型=隐藏名称=商业价值=me@mydomain.com>
    <输入类型=隐藏名称=数量值='。$的价格'>
    <输入类型=隐藏名称=BN值=​​PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest>
    <输入类型=形象SRC =htt​​ps://www.paypalobjects.com/en_US/i/btn/btn_paynowCC_LG.gif边界=0ID =payWithPayPalButtonNAME =提交ALT =贝宝 - 更安全,更方便的网上支付方式>!
    &所述; IMG的alt =边界=0的src =htt​​ps://www.paypalobjects.com/pl_PL/i/scr/pixel.gif宽度=1高度=1>
    < /形式GT;
          ');
?>
 

和Ajax请求的以下内容:

  $。阿贾克斯({
    键入:POST,
    网址:班/ acceptOfferFunction.php
    数据: {
        散列:getURLParameter('散列'),
        HASH2:getURLParameter('HASH2)
    },
    成功:函数(结果){

    }
});
 

解决方案

这里的问题是,你的Ajax请求将被取消,因为该页面将重新加载(事实上重定向到PayPal)。

你可以做的,是送当你提交表单的并等待,直到ajax请求完成Ajax请求。然后,发送表单。

 <形式的行动=htt​​ps://www.sandbox.paypal.com/cgi-bin/webscr的onsubmit =返回ajaxRequest()方法=邮报的ID =payWithPayPalForm>
 

使用的onsubmit ,当用户发送的形式,功能 ajaxRequest 将被解雇。既然我们把回归前,表格将等待函数的返回继续。

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

这是POC。你有更好的方法来处理,在一个非梗阻方式(使用jQuery上的形式的id例如)。而你将不得不$ C C的功能太$。

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) {

    }
});

解决方案

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.

这篇关于PayPal按钮提交表单并触发一个Ajax调用在同一时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆