Jquery 验证远程检查唯一不工作
我想将其发布到网上,因为我已经在这个 JQuery Remote 验证问题上搜索了好几天。我无法让它工作。我认为我的 PHP 代码是正确的,因为我已经使用 URL 中的查询测试了 URL,它返回 false 和 true,具体取决于记录集计数是一个或多个
这是我的 Jquery 验证代码:
// validate form and submit
var $j = jQuery.noConflict();
$j(document).ready(function(){
$j("#myform").validate({
rules: {
ord_ref: {
required: true,
minlength: 12,
remote: "check_ord_ref.php"
},
messages: {
ord_ref: {
remote: "Order Number Does Not Exist"
}
}
}
});
});
< strong>这是我的远程页面“check_ord_ref.php”的 PHP 代码
$colname_rscheck_ord_ref = "-1";
if (isset($_GET['ord_ref'])) {
$colname_rscheck_ord_ref = (get_magic_quotes_gpc()) ? $_GET['ord_ref'] : addslashes($_GET['ord_ref']);
}
mysql_select_db($database_conn, $conn);
$query_rscheck_ord_ref = sprintf("SELECT ref_ord FROM orders WHERE ref_ord = '%s'", $colname_rscheck_ord_ref);
$rscheck_ord_ref = mysql_query($query_rscheck_ord_ref, $conn) or die(mysql_error());
$row_rscheck_ord_ref = mysql_fetch_assoc($rscheck_ord_ref);
$totalRows_rscheck_ord_ref = mysql_num_rows($rscheck_ord_ref);
if($totalRows_rscheck_ord_ref < 0){
$valid = 'false';
} else {
$valid = 'true';
}
echo $valid;
请有人能帮助我和其他有问题的人解决这个难题
- 使用 JQuery 1.5.2min
- 验证正常,无需遥控功能
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,我不是 PHP 专家,但我确实知道 jQuery Validate 期望远程验证方法得到以下结果:
发送
"true"
或"false"
(注意引号)将导致值被解析作为错误消息而不是被评估为布尔原语。回到 PHP 部分,我认为您应该将 json_encode 与布尔原语一起使用。我不太确定在 PHP 中执行此操作的方法,但我相信它会是这样的:
Ok, so I'm no PHP expert, but I do know that jQuery Validate expects the following result from a remote validation method:
Sending down
"true"
or"false"
(note the quotation marks) is going to result in the value being parsed as the error message instead of being evaluated as a boolean primitive.Back to the PHP part, I think you should probably use
json_encode
with a boolean primitive. I'm not quite sure the way to do this in PHP, but I believe it would be something like this:这个问题似乎困扰着远程验证脚本编写者,并且显然缺乏关于此事的 jQuery 文档。
我注意到您正在使用 jQuery 1.5.2:根据我的理解(并从经验中发现),您必须使用 1.4 之后的版本通过 $_REQUEST 发送到远程脚本的 jQuery 回调,并且 jQuery 期望“true”或“ false”作为字符串。这是一个示例,已确认在多种表单上工作(我正在使用 jQuery 1.7.1):
我找到了这个答案 这里(在答案部分),随机的,从那以后就不再拔我的头发了。希望这对某人有帮助。
This problem seems to be plaguing remote validation scripters and the jQuery documentation on the matter is clearly lacking.
I notice you are using jQuery 1.5.2: from what I understand (and found from experience) you must use the jQuery callback that is sent to the remote script with $_REQUEST with versions after 1.4, AND jQuery is expecting "true" or "false" as a STRING. Here is an example, confirmed working on multiple forms (I'm using jQuery 1.7.1):
I found this answer here (in the answers section), randomly, and have since stopped pulling out my hair. Hope this helps someone.
为了补充上面 Andrew Whitaker 的响应,我必须强调,您确定响应严格是 JSON,并且没有返回其他内容类型。我的脚本也遇到了同样的问题,一切似乎都设置正确 - 包括使用 json_encode() 。在使用 Firebug 的 NET 选项卡进行一些故障排除后,我能够确定 PHP 通知被发送回浏览器,将数据从 JSON 转换为文本/html。当我关闭错误后,一切都很好。
To add to Andrew Whitaker's response above, I must stress that you are sure that the response is strictly JSON and that there are no other content types being returned. I was having the same issue with my script, and everything appeared to be set properly - including using
json_encode()
. After some troubleshooting with Firebug's NET tab, I was able to determine that PHP notices were being sent back to the browser converting the data from JSON to text/html. After I turned the errors off, all was well.//check_validate.php
//check_validate.php