I have a problem with jQuery remote validation. I am checking if email is registered, remote validation works, but it display only values - true or false and I cant submit t
For the folks who are not able to get the remote validation working using above technique, following are my two cents which could help ensure you are on right track.
1). It works with v1.6.1 and above only. Stick to latest version of jQuery http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
2). Opt for 'synchronous' remote execution, default being asynchronous. Set async to false.
$("#form").validate({
rules: {
email: {
required: true,
email: true,
remote: { url:"check-email.php", async:false }
}
}
});
It could be just returning strings when a boolean is required. Maybe try the following:
if(mysql_num_rows($checkemail) == 1)
{
$valid = false;
}
else
{
$valid = true;
}
$checkemail = mysql_query("SELECT * FROM users WHERE email = '".$email."'");
Never ever ever ever do this. This is asking for trouble: SQL injection, random errors (the single quote is valid in email addresses, BTW).
There are parameterized queries, use them. It's only a few lines more of code, but it is the difference between a security flaw as wide as a barn door and a safe database interaction.
if(mysql_num_rows($checkemail) == 1)
{
$valid = 'false';
}
else
{
$valid = 'true';
}
is a very verbose way of saying
$valid = mysql_num_rows($checkemail) == 1;
According to the docs, the response of remote validation is a JSON-encoded boolean, not a JSON-encoded string.
You have "true"
or "false"
, which will become "\"true\""
or "\"false\""
through json_encode()
, which is wrong. Actual true
or false
will become "true"
or "false"
, which is correct.
Setting the response content type to JSON might also be a good idea:
header('Content-type: application/json');