I'm using the JQuery Bassistance Form Validation plugin, and everything works fine until I try to check for existing e-mails in my MySQL database.
Here is my code:
$(document).ready(function(){
$('#signup').validate({
rules: {
email: {
required: true,
email: true,
remote: {
type: 'POST',
url: "email_check.php"
}//end remote
}//end email
},//end rules
messages: {
email: {
remote: "Email already in use."
}//end email
} //end messages
}); //end validate
}); //end ready
And here is my email_check.php file:
<? php
$dbc = mysqli_connect('localhost', 'root', '', 'step1_db')
or die('Error connecting to the database');
$email = $_GET['email'];
$check_email = "SELECT * FROM users WHERE email = '$email'";
$existing = mysqli_query($dbc, $check_email);
$num_rows = $existing->num_rows;
if ($num_rows == 1){
echo json_encode(FALSE);
}else{
echo json_encode(TRUE);
}
mysqli_close($dbc);
?>
So right now, if I try to input an e-mail that exists in the database into my form, I get the default error message for invalid e-mail, and not the "Email already in use." Even if I input a valid e-mail that does not exist in the database, the error message doesn't go away.
I'm guessing the problem is in my php file... I've tried using 'return true' and 'return false' but they also did not work. I changed my code to echo json_encode(FALSE) from answers to similar topics as this one. Would anyone be able to put a finger on what's going on here?
From the documentation:
The serverside resource is called via $.ajax (XMLHttpRequest) and gets a key/value pair, corresponding to the name of the validated element and its value as a GET parameter. The response is evaluated as JSON and must be true for valid elements, and can be any false, undefined or null for invalid elements, using the default message; or a string, eg. "That name is already taken, try peter123 instead" to display as the error message.
It looks then like you are responding correctly. It looks like your path to the file is wrong since you are getting a 404 error in the console. FIx the path and the rest looks good.