简体   繁体   中英

Ajax call function on success return false not working

I am running an ajax request, then once I get the result back I choose if it should be continued or if the form should not submit. I am checking if the email exists.

Issue is I moved the return false out of the success: as it was not working there and now in a seperate function it is not working either. I get the alert("FALSE"); but the form still submits which is no good as I want an error pop up to happen.

$.ajax({
  type: "POST",
  url: "/ajax/checkdata.php",
  data: "email="+email,
  success: function(data){
    var returned = true;
    if (data == "Email Exists") {
      returned = false;
    } else {
    }
    emailModal(returned);
  }
})

function emailModal(result){
  if (result) {
    alert("TRUE");
  } else {
    alert("FALSE");
    return false;
  }
}

You'd have to always prevent the form from submitting, and then in the check for the email figure out wether to show an error or submit the form using the native submit handler

$('form').on('submit', function(e) {
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: "/ajax/checkdata.php",
        data: {email : email},
        context: this
    }).done(function(data) {
        if (data == "Email Exists") {
            alert(data);
        } else {
            this.submit();
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM