[英]Issue using remote method in jquery validation
I'm following this article in order to validate my form 我正在关注这篇文章以验证我的表单
My problem is when I have to use the remote
method, eg remote: "check-username.php"
我的问题是当我必须使用remote
方法时,例如remote: "check-username.php"
Since the documentation for remote method is not so clear for me, I would know: 由于远程方法的文档对我来说不是那么清楚,我会知道:
How I need to structure my json output in php script? 我需要如何在PHP脚本中构建我的json输出?
To use the default method for remote
, the remote page must respond with a string value of "true"
or "false"
. 要使用remote
的默认方法,远程页面必须使用字符串值"true"
或"false"
响应。
Valid ( success ) Must be: 有效 ( 成功 )必须是:
echo "true";
Invalid ( failure ): 无效 ( 失败 ):
echo "false"; // this can be any false. eg: 0 , "" , NULL.
The response will be evaluated as JSON. 响应将被评估为JSON。
To apply an error message, other than the default for a remote response of invalid ("false"), add the input name to the validator options messages
object like so. 要应用错误消息(远程响应无效的默认值(“false”),请将输入名称添加到验证器选项messages
对象中,如此。
rules:{
username:{
remote: "check-username.php"
}
},
messages:{
username:{
remote: jQuery.format('{0} is already in use, please choose a different name')
}
}
You don't need JSON. 你不需要JSON。 You can put any text. 你可以放任何文字。 For example you can 例如,你可以
echo "success";
for passed validation, and enter validation message like: 通过验证,并输入验证消息,如:
echo "Username already exists.";
for failed validation. 验证失败。
In your callback do something like this: 在你的回调中做这样的事情:
remote: {
url: "check-username.php" ,
type: "post" ,
data: {
username: function() {
return $("#username").val();
},
complete: function(data){
if( data.responseText != "success" ) {
alert(data.responseText);
//handle failed validation
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.