GVillani82 GVillani82 - 2 months ago 10
Ajax Question

Issue using remote method in jquery validation

I'm following this article in order to validate my form

My problem is when I have to use the

remote
method, e.g.
remote: "check-username.php"


Since the documentation for remote method is not so clear for me, I would know:

How I need to structure my json output in php script?

Answer

You don't need JSON. You can put any text. For example you can

echo "success";

for passed validation, and enter validation message like:

echo "Username already exists.";

for failed validation.

In your callback do something like this:

remote: {
           url: "check-username.php" ,
           type: "post" ,
           data: {
              username: function() {
              return $("#username").val();
           },
           complete: function(data){
                if( data.responseText != "success" ) {
                   alert(data.responseText);
                   //handle failed validation
                }
             }
         }
Comments