title: Return statement executes before ajax response
body: <p>I am making an ajax call on submit button click event to check field validations server side. 
  When I get a validation fail, ajax response gives the proper error message, returns false, and stops the form to submit to the action url.  But when I get a response of 'success', the form is still not submitting to the action url script.  </p>
  
  <p>Is this the case when return statement executes before ajax response?</p>
  
  <p>And also why is the form not getting submitted?</p>
  
  <p>Here is the code: </p>
  
  <pre><code>&lt;input type="submit" onclick="return validate();" name="submit" value="Proceed" /&gt;
  
  &lt;script type="text/javascript"&gt;
      var flag=false;
      function validate(){
  
          $.ajax({
              type:"POST",
              url:"../chk.php",
              data:datastring,
              cache: false,
              success: function (result) {
  
  
                  if(result.toString() == "success" ){
  
                      flag=true;
  
                  }
  
                  else{
                      $('#error').css('display', 'block');
                      $('#error').css('color','red');
                      $('#error').text(result.toString());
                      flag=false;
                  }
              }
  
          });
  
  
          return flag;
      }
  &lt;/script&gt;
  </code></pre>