jQuery 远程验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5178012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Remote validation
提问by iKaspars
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 the form than.
我有 jQuery 远程验证的问题。我正在检查电子邮件是否已注册,远程验证是否有效,但它仅显示值 - 真或假,我无法提交表单。
jQuery code :
jQuery 代码:
$("#form").validate({
rules: {
email: {
required: true,
email: true,
remote: "check-email.php"
}
}
});
check-mail.php code :
check-mail.php 代码:
$email = trim(strtolower($_REQUEST['email']));
$checkemail = mysql_query("SELECT * FROM users WHERE email = '".$email."'");
if(mysql_num_rows($checkemail) == 1)
{
$valid = 'false';
}
else
{
$valid = 'true';
} //end of $checkemail if statemant
echo json_encode($valid);
回答by Tomalak
$checkemail = mysql_query("SELECT * FROM users WHERE email = '".$email."'");
Never ever evereverdo this. This is asking for trouble: SQL injection, random errors (the single quote is valid in email addresses, BTW).
永远永远永远永远做到这一点。这是自找麻烦:SQL 注入、随机错误(单引号在电子邮件地址中有效,顺便说一句)。
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 veryverbose 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.
根据文档,远程验证的响应是 JSON 编码的boolean,而不是 JSON 编码的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.
你有"true"
or "false"
,这将变成"\"true\""
or "\"false\""
through json_encode()
,这是错误的。实际true
orfalse
将变成"true"
or "false"
,这是正确的。
Setting the response content type to JSON might also be a good idea:
将响应内容类型设置为 JSON 也可能是一个好主意:
header('Content-type: application/json');
回答by Ewan Heming
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;
}
回答by Design Kanya
For the folks who are not able to get the remote validationworking 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
1)。它仅适用于 v1.6.1 及更高版本。坚持使用最新版本的 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.
2)。选择“同步”远程执行,默认为异步。将异步设置为 false。
$("#form").validate({
rules: {
email: {
required: true,
email: true,
remote: { url:"check-email.php", async:false }
}
}
});