Coder
Coder

Reputation: 21

Submit a form after finished Ajax request

I want to submit a signup form. In the form onload I called one JavaScript function to validate all the fields.

Here I should call one ajax function to validate one field.

var checkExists = true;

checkExists = ajaxQuery(data);

if (checkExists) {

    alert('Already exists');

   return false;

}

ajaxquery is an ajax function which will return true or false based on the result. My problem is that no matter what the AJAX result is, it will first execute the if condition and return false.

So my form always gets stuck even checkExists variable value is false. Please help

Upvotes: 1

Views: 932

Answers (1)

James.Xu
James.Xu

Reputation: 8295

because ajax query is asychronous, you need to put your code in the callback method of ajaxQuery: http://api.jquery.com/jQuery.ajax/

$.ajax({
  url: "test.html",
  context: document.body,
  success: function(data){
     // your logic goes here.
     alert('Already exists');
     return false;
  }
});

Upvotes: 3

Related Questions