开发者

Posting two forms at once

开发者 https://www.devze.com 2023-02-07 12:06 出处:网络
I have two forms on my page - an item selection form that interacts with a database of mine, and a quantity selection form that interacts with Paypal.

I have two forms on my page - an item selection form that interacts with a database of mine, and a quantity selection form that interacts with Paypal.

One has post to ""开发者_高级运维, the other posts to Paypal's site. I want them both to post at once, but only when the Paypal button is pressed, so that my code runs, and the page goes to Paypal.

How can I do this?


You have at least a couple options.

You could:

  1. Spawn an AJAX request to post data to your server, then submit the paypal form as the success callback of this AJAX request.
  2. Submit the form to your server, and have the response of the form submission redirect to a page which knows how to submit a post request to Paypal.

Of the two, I would recommend 1) as you can easily control the UI the user is experiencing and you do not create oddly related components on your server.

For 1, note that test.php is an absolute or relative url to your serverside script, testform is the id of the form you want to submit to your server, and your_paypal_form is the id of the form you want to submit to paypal:

$('#paypalButton').click(function(){
   $.post("test.php", $("#testform").serialize(), function(){
      $('#your_paypal_form').submit();
   });
 });
0

精彩评论

暂无评论...
验证码 换一张
取 消