-2

This is my Script :

<script>
   $(function() {
       $("#yourFormId").validate({

          rules: {
              mobile: {
                  required: true,
                  mobile: true,
                  remote: {
                     url: "mobileajax.php",
                     type: "get"
                  }
              },
           },

          messages: {

             mobile: {
                required: "Please enter a valid mobile address",
                minlength: "Please enter proper mobile",
                remote:function(){
                    $('.result').html('mobile Already used.').addClass('messageboxerror').fadeOut(5000);
                },
            }
         },

         errorPlacement: function (error, element) {
            element.attr("placeholder", error.text());
         },
   });

}); 
</script>

This is Html file:

<form id="yourFormId" action="try.php" method="post">
   mobile No.<div class="result"></div>
    <input id="mobile" type="text" name="mobile" />
<input type="submit" value="submit">

Thsi is mobileajax.php

 <?php
    require "connect/connectOpen.php";
    $sql = "select mobile from tbl_user";
    $rsd = mysql_query($sql);
    $data = mysql_fetch_array($rsd); 

    $registeredEmail = array($data[0]);

    $requestedEmail  = $_REQUEST['mobile'];

    if( in_array($requestedEmail, $registeredEmail) ){
        echo 'false';
    } else {
        echo 'true';
    }
?>

Same things i was using for email match exist its works, but for mobile its did not work.not validate moile number from database, only same code works with email match.

Leo Silence
  • 1,192
  • 11
  • 22
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 19 '15 at 12:39

1 Answers1

0

Why did the above code didn't work? The mysql result was immediately used to find for the mobile for testing/checking. The result should be have been added into an array by traversing through it first, and using that array to check for the mobile if it's in the table:

while($row = mysql_fetch_array($rsd)) {
     $data[] = $row;
}

Try doing this instead:

    <?php
        require "connect/connectOpen.php";
        $sql = "select mobile from tbl_user where mobile = '".mysql_real_escape_string($_REQUEST['mobile'])."'";
        $result = mysql_query($sql);
        $num_rows = mysql_num_rows($result);

        if ($num_rows == 1) {
             echo 'false';
        } else {
             echo 'true';
        }
    ?>

I would also suggest that you use mysqli

denil
  • 690
  • 5
  • 8
  • @ShoaibMansuri You're welcome. I've updated my answer to explain where the mistake of your original code is. This is also for the benefit of other searching for solutions :) – denil Jun 11 '15 at 07:28