How to check if mobile number is already in use using jquery and ajax?

This is how I am validating my form fields using jquery

    $(document).ready(function () {
        $(function() {


              $("form[name='cont']").validate({
             // Specify validation rules
             rules: {
 
                      full_name: "required",
                      address: "required",
                     collage: "required",
                    grade: "required",
                     percentage: "required",

                     student_mobile:{
                                       required: true,
                                       minlength:10, 
                                         remote: {
                                              url: "submit.php",
                                              type: "post"
                                          },
                                      },

                     parent_mobile:{
                                required: true,
                                minlength:10,
                           },
  

        },
// Specify validation error messages
 messages: {
  full_name: "Please enter your full name",
  address: "Please enter your address",
  collage: "Please enter your collagel name",
  grade: "Please select grade,
  percentage: "Please enter your percentage",
  student_mobile:{
               required:"Please enter your mobile number",
               minlength: "Mobile number must be of 10 digits",
               remote: "Mobile number is already in use!"
            }, 
 parent_mobile:{
               required:"Please enter your mobile number",
               minlength: "Mobile number must be of 10 digits"
            },
 
},


submitHandler: function(form) {
      send();
     }

      });
    });
 });

This is my submit.php file where I am trying to compare mobile number

       <?php 



           $conn = mysqli_connect("localhost", "root", ""); // Establishing Connection with Server..
           $db = mysqli_select_db($conn,"new"); // Selecting Database

         // Check connection
         if ($conn->connect_error) {
           die("Connection failed: " . $conn->connect_error);
         }
         if (isset($_POST['submit'])) {
                    $student_mobile = $_POST['student_mobile'];
             
             if( $student_mobile !=''){
               $check_mobile = mysqli_query($conn,"SELECT student_mobile FROM univ 
                             WHERE student_mobile =".$student_mobile);
             $res = mysqli_fetch_row($check_mobile);
            }
         $num_rows = count($res);
         if( $num_rows > 0 ){
         echo 'false';
        } else {
               //insert query here 
               }
         }
  • My other validations are working fine but validation for mobile is not
    Where I am going wrong? I am confused , please help me

What does “not working” mean? Does it give an error?
Can you tell us what the script should do, and what it does instead?

thanks for the reply ,

  • Actually the script is supposed to give an error if there is mobile number which is already benn used
    -but my other validations are working except for this ,
  • Its not giving message if same mobile number is used or not
  • I want to fix it , but unable to do it , can you please guide me ?

Is it actually running your query? I don’t know the form validator you’re using, but does it send all the form data on validation, or just the field you are validating? Specifically, will this line

  if (isset($_POST['submit'])) {

cause the problem? The doc (if I’ve found the correct code) says “The serverside resource is called via jQuery.ajax (XMLHttpRequest) and gets a key/value pair corresponding to the name of the validated element and its value as a GET parameter.”, though obviously you’ve changed that to POST in the code. That suggests to me that it is only sending student_mobile in this instance.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.