0

Possible Duplicate:
How to return the response from an AJAX call from a function?

i am creating a registration page with email id and password . i have called onsubmit event in registration page which call's checkData() function which is written in a common.js file. i have a ajax function to check whether email id exist in database or not ? but my problem is when i keep alert message after the completion of checking my ajax function works fine but if alert is removed it enter's data in to database. here is the code of ajax

   else
   {
       $(function() {

              $.ajax({
                    url  : 'RegCheck.php',
                    type : 'POST',
                    data : 'User=' + address,
                    success : function(result){
                       if(result != "No")
                       {
                           document.getElementById("EmailChk").innerHTML = result;    
                           Valid = false;

                       }
                    }

              });              


       });                         
      document.getElementById("MailMand").innerHTML = "" ;

   }    
   alert(Valid);
   return Valid;
Community
  • 1
  • 1

1 Answers1

1

ajax doesn't work that way. The success code will execute after the return Valid code 99.9% of the time (if not 100% of the time). You can't guarantee when the success will be called.

Instead, you need to do all of your work in the ajax callback or (shudder) make it synchronous.

So if you're depending on it for validation, you need to stop the form from submitting and then submit the form in the success method.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • but when i put an alert message after the completion of ajax my result shows as already registed email but if i remove the alert message it fire's my insert query – bharat patel Jan 24 '13 at 07:27