AKor
AKor

Reputation: 8882

Posting two forms simultaneously using Ajax/jQuery

I have a form that posts to post.php, and a form that posts to Paypal. I want it so that when I click on the Paypal submit button, it posts both forms and goes to the page where the Paypal button would take you.

However, I ran into a wall with this and I'm not sure what the cause is. The form goes to the Paypal page fine, but doesn't POST my other, non-paypal form.

Here's my JS: http://slexy.org/view/s2u2Jsr2RI

Here's my HTML: http://slexy.org/view/s2gc4q8CHe

Here's my post.php: http://slexy.org/view/s208WfbKso

Here are the lines of jQuery which I believe to be the root of the issue:

$('#paypalButton').click(function(){
     $.post('post.php', $('#myForm').serialize(), function(){
               $('#paypal').submit();
    });
});

Upvotes: 1

Views: 948

Answers (2)

CheezWeezil
CheezWeezil

Reputation: 1

This works: just prevent the event's default action of posting the form.

$(function(){
    $('#paypalButton').click(function(**event**){
        **event.preventDefault();** 
         $.post('post.php', $('#myForm').serialize(), function(){
               $('#paypal').submit();
         });
    });
});

Upvotes: 0

David Pean
David Pean

Reputation: 2906

The paypal form is being submitted first because you have the input field as a type="image". That acts as a submit button

Upvotes: 2

Related Questions