Skip to content Skip to sidebar Skip to footer

Using The Jquery Validate Plugin With The "remote" Option

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/ch

Solution 1:

Try changing:

echo("username is already exists");

to

echo("false");

Solution 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) 
ordie('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"
     }
   }
})

Solution 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.

Post a Comment for "Using The Jquery Validate Plugin With The "remote" Option"