使用带有“remote”选项的jQuery validate插件

时间:2011-07-21 06:33:35

标签: php javascript jquery validation plugins

我正在使用jQuery validate插件及其远程选项来检查输入字段中是否存在用户名。我使用这段代码:

 remote : {
      url :'ajax_php/admin/checking.php',
      type   :'post',
      data   : {
      type:'username'   
}}

我注意到请求URL附加了一个回调参数,即使我将类型请求设置为post

http://localhost/lopoli2/ajax_php/admin/checking.php?callback=jQuery15104128487491980195_1311232389069

我的PHP脚本运行正常,并返回true表示有效的用户名和无效用户名的字符串。但是没有出现错误信息!简而言之,我的PHP文件如下所示:

$check = mysql_query("select `username` from `user_tbl` where `username`='".$_POST['username']."' ",$conn) 
or die('Error In DB !');

if (mysql_num_rows($check)>0){
    echo("username is already exists");
}else{
    echo("true");
}

这是我想知道的:

  • 回调参数是什么?
  • 如何解决“显示错误信息”问题?

2 个答案:

答案 0 :(得分:0)

尝试更改:

echo("username is already exists");

echo("false");

答案 1 :(得分:0)

我认为你必须在php脚本中返回true或false。然后,您可以使用选项对象中的messages参数使用自定义错误消息来设置错误消息。

另外,我认为您的数据参数配置错误。它在文档中看起来像这样。虽然,你提到它有用,所以也许不同的版本? (http://docs.jquery.com/Plugins/Validation/Methods/remote#options)

remote: {
    url: 'ajax_php/admin/checking.php',,
    type: "post",
    data: {
        username: function() {
            return $("#username").val();
        }
    }
}

然后在你的php脚本中返回'true'或'false'。您可以使用jquery validate plugin

设置错误消息
$check = mysql_query("select `username` from `user_tbl` where `username`='".$_POST['username']."' ",$conn) 
or die('Error In DB !');

if (mysql_num_rows($check)>0){
    echo 'false';
}else{
    echo "true";
}

以下是添加消息参数的方法(http://docs.jquery.com/Plugins/Validation/validate#options)

$(".selector").validate({
   rules: yourRules,
   messages: {
     username: {
       remote: "Username already exists"
     }
   }
})
相关问题