Using the jQuery validate plugin with the “remote

2019-09-08 23:25发布

问题:

I am using the jQuery validate plugin and its remote option for checking the existence of a username from an input field. I use this code:

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

I've noticed that the request URL has a callback parameter appended, even though I set the type request to post:

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

My PHP script works fine and returns true for valid username and a string for invalid usernames. But no error message appears! My PHP file in short looks like this:

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

Here's what I want to know:

  • What is the callback parameter for?
  • How to solve the "display error message" problem?

回答1:

Try changing:

echo("username is already exists");

to

echo("false");


回答2:

I think you have to return true or false in your php script. Then you can set your error message by using a custom error message using the messages parameter in your options object.

Also I think your your data param is configured wrong. It looks like this in the docs. Although, you mention it works, so maybe different version? (http://docs.jquery.com/Plugins/Validation/Methods/remote#options)

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

Then in your php script return 'true' or 'false'. You can set the error message using the 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";
}

Here is how you add a message parameter (http://docs.jquery.com/Plugins/Validation/validate#options)

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


回答3:

The returned value is considered to be a boolean (true/false) or a string (will be handled as false and display the returned text).

Also, it is important to set the output to text/javascript or text/plain using header('content-type:text/plain');

So in php, when returning the value, you should pay attention to the type of the returned value as follows:

echo 'true'; //=> true, a boolean. Says the result of the validation is true and everything is okay.

echo 'false'; //=> false, a boolean. Says the result of the validation is wrong and display a message configured in the validator object

echo '"false"'; //=> "false", a string. Says the result of the validation is wrong and display the returned string will be displayed as an error.