Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Jquery and AJAX [duplicate]

Tags:

jquery

ajax

php

I am using AJAX to call a PHP file that does email validation and outputs simple text that gets returned to the AJAX Success. I am using the text returned to create variables to represent which error to display. I am having trouble using these variables, the form still submits. What could I be doing wrong?

My Code:

if (email != 0) {
// Run AJAX email validation and check to see if the email is already taken
$.ajax({  
    type: "POST",  
    url: "checkemail.php",  
    data: dataString,
    success: function(data) {
        if (data == 'invalid') {
            var invalid= 1;
        }
        else if (data == 'taken') {
        var taken= 1;
        }
    }
});
}

if (invalid == 1) {
    alert('invalid email');
error= true;
}

if (taken == 1) {
    alert('email taken');
error= true;
}

if (error == true) {
    return false;
}
like image 899
user3191005 Avatar asked Feb 07 '26 02:02

user3191005


1 Answers

Is this AJAX call inside a click event? If so, you may want to use event.preventDefault() function to avoid triggering the form submission:

event.preventDefault();

Have a look to the example in the documentation

like image 64
dcapilla Avatar answered Feb 09 '26 15:02

dcapilla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!