2

I am having problem in signup.php page. I want if information is already existing in database then this page should stop redirecting to next page and show alert box. Now the problem is I tried the following code snippet of jquery but this did not work in my case.I googled about this and come with the following solution but this did not work.

Jquery Code

function validate()
{
    var email =$('#email').val();
    var mobile = $('#mobile').val();

    $.post("check_user.php",{ email: email,mobile:mobile}, function(result){
        if(result == "0"){
            alert('This email is already registered with us. Please try another.');
            return false;
        }
        else
        {
            return true;
        }
    });
}

Html code is given below where I am calling this jquery function

HTML Code

<form id="form1" autocomplete="on" action='/marammut_new/test.php'
method="post" enctype="multipart/form-data" onsubmit="return validate()"> 
...
</form>
Danscho
  • 466
  • 5
  • 20
RaviKant Hudda
  • 1,042
  • 10
  • 25
  • Did you mean the alert show up or it does not show up? If it does not show then... Please paste your check_user.php code too.. – DpEN May 23 '16 at 06:31
  • Have you checked the 'result' by alerting before checking it for value? like, function(result) { alert(result); .... – Shwetha May 23 '16 at 06:37

3 Answers3

2

As this is a asynchronous call and it validates the form asynchronously and before the response it would get the function gets executed. Better to stop the form submission and submit it explicitly:

function validate() {
  var email = $('#email').val();
  var mobile = $('#mobile').val();

  $.post("check_user.php", { email: email, mobile: mobile },function(result) {
      if (result == "0") {
        alert('This email is already registered with us. Please try another.');
      } else {
        $('#form1')[0].submit();
      }
    });
  return false; // stop it here
}
Jai
  • 74,255
  • 12
  • 74
  • 103
0

You should try Window.location.href It works for me.

Anoop Mishra
  • 1,005
  • 2
  • 10
  • 33
  • What if my condition fails and I want my information to be retained on the page. I have tried your code but it did not work me but this might be good solution for someone else. – RaviKant Hudda May 23 '16 at 06:19
0

First of all, you are returning true/false inside an Asynchronous function, so the onSubmit function never get the return.

Second you want to submit the form depending on the result of the asynchronous call, so you should always prevent the default behaviour of the onSubmit function and perform the corresponding action after successful execution of the asynchronous request.

This is the code snippet that will submit the form after successfully request with positive response but alert an error otherwise.

function validate(e) {
    // prevent the default behaviour of submit event
    e.preventDefault();
    var email =$('#email').val();
    var mobile = $('#mobile').val();

    $.post("check_user.php",{ email: email,mobile:mobile}, function(result){
        if(result == "0"){
            alert('This email is already registered with us. Please try another.');
            return false;
        } else {
            // submit the form if it is valid
            this.submit();
            return true;
        }
    });
    // always return false to prevent the default behaviour of submit event
    return false;
}
Calvin Lau
  • 547
  • 1
  • 4
  • 11