0

I have two forms on one page (one generated by one service so I can't change it). I need when I sumbit form1 then firstly I send with Ajax, second form an after send second form send first form. I try to do it this way:

$('#form1').submit(function(event) {
            var this = (this);
            event.preventDefault();           
            $.ajax({
                type:"POST",
                url: "/wp-admin/admin-ajax.php",
                data: $('#form2').serialize(),
                success:function(data){
                    this.unbind('submit').submit();
                }         
            });
        }); 

The problem is, that I can't send first form after second one. Ajax ends with success, but first form are not send.

Thanks for help

1
  • 1
    Please spell out more clearly the desired order of events. Commented Oct 11, 2014 at 21:05

3 Answers 3

1

You have to use promise to make it convenient.

function submitForm1 () { 
 return $.ajax({
                type:"POST",
                url: "/wp-admin/admin-ajax.php",
                data: $('#form1').serialize(),        
            });
}); 


function submitForm2 () { 
 return $.ajax({
                type:"POST",
                url: "/wp-admin/admin-ajax.php",
                data: $('#form2').serialize(),       
            });
});



submitForm2().done(function () {
    submitForm1();
});
Sign up to request clarification or add additional context in comments.

1 Comment

The question does not indicate that form1 should be submitted with AJAX.
0

Two things :

  • $.ajax() returns a promise, so you can chain .then(fn), which can make life easier with regard to the meaning of this.
  • form.submit() will not retrigger the form's submit handler, therefore you don't need to unbind it.

Try this :

$('#form1').on('submit', function(event) {
    event.preventDefault();
    $.ajax({
        type: "POST",
        url: "/wp-admin/admin-ajax.php",
        data: $('#form2').serialize()
    }).then(this.submit.bind(this));
});

Comments

0

Your form does not get sent because the this keyword in your success function will not be referencing your form element and so your function calls with it are erroring out.

Also this is a reserved word you cannot use it as a variable

var this = (this);
//change to
var that = this;

You can set the execution context of the success function a couple ways

  1. Pass the context in the ajax options

    jQuery.ajax({
       //...
       context: jQuery("#form1"),
       success:function(data){
          this.unbind("submit").submit();
       }
    })
    
  2. Use the bind function

    jQuery.ajax({
        //...
        success: function(data){
           this.unbind("submit").submit();
        }.bind(jQuery("#form1"))
    });
    
  3. Use a saved reference of this

    var that = this;
    jQuery.ajax({
       //...
       success:function(data){
           jQuery(that).unbind("submit").submit();
       }
    });
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.