如何响应jQuery Validation remote? [英] How to respond to jQuery Validation remote?
问题描述
查看了多个SO问题和手册,但找不到它.当我有
Looked at multiple SO questions and the manual, but can't find it out. When I have
$( "#myform" ).validate({
rules: {
email: {
required: true,
email: true,
remote: "check-email.php"
}
}
});
我的check-email.php
应该如何回应?我已经尝试过echo "true"
,echo true
,echo {error: true}
,但是没有给出正确的错误消息.
How should my check-email.php
respond? I already tried echo "true"
, echo true
, echo {error: true}
, but it's not giving the right error message.
编辑
我目前将我的php设置为:
I currently have my php set up as:
$query = mysql_query("SELECT id FROM users WHERE email='".$_POST['email']."'") or die (mysql_error());
if(mysql_num_rows($query)!=0){
echo json_encode(false);
}else{
echo json_encode(true);
}
但是,它总是返回true ...
However, it's always returning true...
推荐答案
引用OP:
我的
check-email.php
应该如何回应?我已经尝试过echo "true"
,echo true
,echo {error: true}
,但未给出正确的错误 消息.
How should my
check-email.php
respond? I already triedecho "true"
,echo true
,echo {error: true}
, but it's not giving the right error message.
这是因为true
不会触发错误消息...这是相反的响应.
That's because true
will not trigger an error message... it is the opposite response.
-
true
是有效时的正确响应(无错误消息).
true
is the proper response for when it's valid (no error message).
如果响应为false
,null
,undefined
或字符串(错误消息),则该字段被评估为无效.
If the response is false
, null
, undefined
, or a string (error message) the field is evaluated as invalid.
引用标题:
如何响应jQuery Validation remote?
How to respond to jQuery Validation remote?
服务器端资源通过jQuery.ajax(XMLHttpRequest)和 获取与已验证名称对应的键/值对 元素及其值作为GET参数. 响应的评估为 JSON和对于有效元素,必须为
true
,并且可以为任意false
,undefined
或null
用于无效元素,使用默认消息;或者 字符串,例如该名称已被使用,请尝试peter123" 显示为错误消息.
The serverside resource is called via jQuery.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 anyfalse
,undefined
ornull
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.
编辑:
根据编辑和评论...
As per edits and comments...
-
OP在他的PHP中使用
$_POST['email']
.但是,以上文档指出remote
方法默认情况下使用GET
.
OP is using
$_POST['email']
in his PHP. However, the documentation above states that theremote
method usesGET
by default.
如果您要自定义错误消息,则需要遵循上述文档...因此在您的PHP中,而不是返回false
...构造消息并在字符串中返回. /p>
If you want a customized error message, then you need to follow the documentation above... so within your PHP, instead of returning false
... construct the message and return it within a string.
这篇关于如何响应jQuery Validation remote?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!