Jquery 验证远程检查唯一不工作

发布于 2025-01-01 08:09:37 字数 1340 浏览 0 评论 0 原文

我想将其发布到网上,因为我已经在这个 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;

请有人能帮助我和其他有问题的人解决这个难题

  1. 使用 JQuery 1.5.2min
  2. 验证正常,无需遥控功能

I wanted to post this online because I have been searching for days on this JQuery Remote validation issue. I cannot get it to work. I think my PHP code is correct as I have test the URL with a query in the URL and it returns false and true depending on with the recordset count is one or more

This is my Jquery Validate Code:

// 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"
}
}
}
});
});

This is my PHP code for the remote page "check_ord_ref.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;

Please someone can you help solve the puzzle for myself and anyone else having issues

  1. Using JQuery 1.5.2min
  2. Validates OK without remote function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野鹿林 2025-01-08 08:09:37

好的,我不是 PHP 专家,但我确实知道 jQuery Validate 期望远程验证方法得到以下结果:

响应被评估为 JSON 并且对于有效元素必须为 true,
对于无效元素,可以是任何 false、未定义或 null

发送 "true""false" (注意引号)将导致值被解析作为错误消息而不是被评估为布尔原语。

回到 PHP 部分,我认为您应该将 json_encode 与布尔原语一起使用。我不太确定在 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; // <-- Note the use of a boolean primitive.
} else {
    $valid = true;
}

echo json_encode($valid);

Ok, so I'm no PHP expert, but I do know that jQuery Validate expects the following result from a remote validation method:

The response is evaluated as JSON and must be true for valid elements,
and can be any false, undefined or null for invalid elements

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:

$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; // <-- Note the use of a boolean primitive.
} else {
    $valid = true;
}

echo json_encode($valid);
黑色毁心梦 2025-01-08 08:09:37

这个问题似乎困扰着远程验证脚本编写者,并且显然缺乏关于此事的 jQuery 文档。

我注意到您正在使用 jQuery 1.5.2:根据我的理解(并从经验中发现),您必须使用 1.4 之后的版本通过 $_REQUEST 发送到远程脚本的 jQuery 回调,并且 jQuery 期望“true”或“ false”作为字符串。这是一个示例,已确认在多种表单上工作(我正在使用 jQuery 1.7.1):

if($totalRows_rscheck_ord_ref < 0){
    header('Content-type: application/json');
    $valid = 'false'; //    <---yes, Validate is expecting a string
    $result = $_REQUEST['callback'].'('.$check.')';
    echo $result;
} else {
    header('Content-type: application/json');
    $valid = 'true'; //    <---yes, Validate is expecting a string
    $result = $_REQUEST['callback'].'('.$check.')';
    echo $result;
}

我找到了这个答案 这里(在答案部分),随机的,从那以后就不再拔我的头发了。希望这对某人有帮助。

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):

if($totalRows_rscheck_ord_ref < 0){
    header('Content-type: application/json');
    $valid = 'false'; //    <---yes, Validate is expecting a string
    $result = $_REQUEST['callback'].'('.$check.')';
    echo $result;
} else {
    header('Content-type: application/json');
    $valid = 'true'; //    <---yes, Validate is expecting a string
    $result = $_REQUEST['callback'].'('.$check.')';
    echo $result;
}

I found this answer here (in the answers section), randomly, and have since stopped pulling out my hair. Hope this helps someone.

小伙你站住 2025-01-08 08:09:37

为了补充上面 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.

庆幸我还是我 2025-01-08 08:09:37

//check_validate.php

<?php

// some logic here

echo json_encode(true);
?>

//check_validate.php

<?php

// some logic here

echo json_encode(true);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文