Philip Mc Lennan
Philip Mc Lennan

Reputation: 21

Submit the form based on ajax result

I have a page that I want to manually or pro-grammatically submit. But before the page is allowed to be submitted, a functions [create_impi_num(x)] need to kick of to store data using "$.post('" in that function .

The Problem: IF I set the submit(function(e) to return false the functions are called and my data is stored. IF I set the return to true, my form is posted but my functions are ignored.

Here is my Code:

$('#pay-and-complete').submit(function(e) {

 var ok = confirm('Do you really want to save your data?');
    if (ok) {

     var CanPay;
     var igroupid =  Number($('#hid-step3-entryid').val());

     //function here
      create_impi_num($('#hid-parc-id').val(),$('#hid-step3-entryid').val());

      return true;
   }
    else {
     //Prevent the submit event and remain on the screen
     create_impi_num($('#hid-parc-id').val(),$('#hid-step3-entryid').val());
      e.preventDefault();
      return false;
    }

  });

I have spend hours trying to find a solution. If anyone can help that would be greatly appreciated!

Upvotes: 2

Views: 102

Answers (1)

gdoron
gdoron

Reputation: 150253

It simply can't be done the way you want.

jQuery post function excutes ayncrounsly meaning it will return sometime in the furture, in the meanwhile the code continue to be executed. Until the result returned the submit function is already done.

What you can do is on the post callback if you need to submit, use javascript native functions to submit or redirect.

Upvotes: 2

Related Questions