I have a PHP script that checks an email address to determine if it is valid or not. The function simply returns true or false. I am trying to implement an ajax call to the php file and return the true or false back to the javascript function. Currently I am getting nothing.
function validateForm() {
var email = document.forms["contact"]["email"].value;
var validEmail;
$.ajax({
type: "POST",
url: "validateEmail.php",
data: "email=" + email,
success: function(result) {
validEmail = result;
console.log('result: ' + validEmail);
}
});
}
If I can retrieve the true or false I will then do some boolean checking to determine how to proceed. The function validateForm() is being called when the user clicks a form Submit button.
<form name="contact" action="submitOrder.php" onsubmit="return validateForm();" method="post">
I’d like the validateForm() function to return false if the email address is not valid so that submitOrder.php is never called but due to the asynchronous nature of AJAX, I am not sure how to do this.
UPDATE
The request is working correctly (I’m pretty sure). This is what I am seeing in Firebug.

I’m still not seeing anything returned. The console prints “result: “. I also changed validateEmail.php to return the number 17 and I still get nothing returned.
Thanks for any help!
Use JQuery Validate plugin. Would be a lot simpler then hashing this out from scratch.