0

I have a form with two fields User ID and Email ID. I am trying to validate value of each field is already taken or not using same php file with jQuery remote validation.

But my confusion is how should be the remote file in my case (validate.php). How it can determine the fields.

My code looks like:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Validationt</title>


<script type="text/javascript">
$(document).ready(function(){
   $("form").validate({
      rules: {
         userid: {
            required: true,
            remote: {
               url: "validate.php",
               type: "post",
             }
         }
          emailid: {
            required: true,
            remote: {
               url: "validate.php",
               type: "post",
             }
         }
      },
      messages: {
         userid: {
            remote: jQuery.validator.format("userid {0} is already taken")
     }
         emailid: {
            remote: jQuery.validator.format("emailid {0} is already taken")
     }
      }
   });
});
</script
</head>
<body>
<form method="post" id="form" action="">
<input id="userid" name="userid"  type="text" />
<input id="emailid" name="emailid" type="text" />
<input type="submit" name="submit" id="submit"/>
</form>
</body>
</html>

Any help ?

klari
  • 25
  • 2
  • 5

3 Answers3

1

Like,

//validate.php

//get the post fields
$email_address = trim( $_POST["emailid"] );
//check if email exists against database, like
if( is_valid_from_db( $email_address ) ) {
  return TRUE;
}
else {
  return FALSE;
}
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • Actually I know how to make remote validation for a single field. But my problem is where I want to make remote validation for multiple fields using the same remote file (in my case validate.php). In this case what modification is needed to file validate.php to determine each fields separately. – klari Jan 16 '13 at 12:45
  • @klari, you could make the url like: validate.php?check_for=emailid and then in your validate.php file check for the "check_for" data and perform the validations for emailid and userid according to the value set in $_GET["check_for"] .. hope that works – Sudhir Bastakoti Jan 17 '13 at 03:28
0

I am sure you have looked at the docs: http://docs.jquery.com/Plugins/Validation/Methods/remote#options

validate.php just needs to do what it says validate the fields.

This other question might be of some help: jQuery Remote validation

Community
  • 1
  • 1
matpol
  • 3,042
  • 1
  • 15
  • 18
  • Your replay is also about a single field validation, actually I know how to do it. But my problem is where I want to make remote validation for multiple fields using the same remote file (in my case validate.php). In my above code I use tow fields validate with the same php file In this case what modification is needed to file validate.php to determine each fields separately – klari Jan 16 '13 at 15:29
  • its the same as doing it server side - you get the fields from the post validate them then return something - in this case the output needs to be json or html. – matpol Jan 17 '13 at 09:15
0

I was also looking for an answer for this problem of validating multiple fields with remote method.

To determine each field separately, you don't have to define remote method for each field. Whereas you can define remote method for your 'emailid' field and you can send 'userid' alongside the emailid using data option as follows.

            emailid: {
                required: true,
                remote: {
                   url: "validate.php",
                   type: "post",
                   data: {
                       userid: function() {
                       return $("#userid").val();
                      }
                   }
                 }
             }

So you can have your js like this

<script type="text/javascript">
   $(document).ready(function(){
   $("form").validate({
      rules: {
         userid: {
            required: true
         }
          emailid: {
            required: true,
            remote: {
               url: "validate.php",
               type: "post",
               data: {
                   userid: function() {
                   return $("#userid").val();
                  }
               }
             }
         }
      },
      messages: {
         userid: {
            remote: jQuery.validator.format("userid {0} is already taken")
     }
         emailid: {
            remote: jQuery.validator.format("emailid {0} is already taken")
     }
      }
   });
});
</script>

The above will post userid and emailid to your validate.php where you can follow DemoUser's answer. But make sure the returning values (true/false) are string values

user27
  • 169
  • 1
  • 9