How to wait for ajax validation to complete before submitting a form?

前端 未结 7 865
悲哀的现实
悲哀的现实 2020-12-20 23:00

Having a problem where the form submits before the validateUsername function has a chance to complete the username check on the server-side.

How do I submit the form

7条回答
  •  情话喂你
    2020-12-20 23:19

    In jQuery there is a solution for the same. I need to check if a user exists or not in my application during some JavaScript. Here is the code:

    var pars = "username="+ username;
    var some = $.ajax({url: 'AjaxUserNameExist.php',
      type: 'GET',
      data: pars,
      async: false
    }).responseText;
    
    
    if(some=="1") //this is value Got from PHP
    {
      alert("We cannot Go");
      return false;
    }
    
    if(some=="0")
    {
      alert("We can Go");
      return true;
    }   
    

提交回复
热议问题