Stop record from submitting if mobile no already exist in database table

I have small registration form which have following fields Idno, Name and Mobile No. I want to knew that How can I stop my form from submitting and generating an error if mobile no already existed in database . I tried with ajax but its not working Here is code of my simple form


		 <form method="POST" action="form1.php">	
						
				
		<div class="row"> <div class="col-lg-4"> <div class="form-group">
		<label>ID No:</label> <input type="text" name= "idno"  id= "idno" class="form-control" >
		</div> </div> 
<div class="col-lg-4"> <div class="form-group">
		<label>Name:</label> <input type="text" name= "name"  id= "name" class="form-control" Required >
		</div> </div>
<div class="col-lg-4"> <div class="form-group">
		<label>Mobile No:</label> <input type="text" data-inputmask="'mask': '0399-99999999'" name= "mob"  id= "mob" class="form-control mobunique">
		</div> </div></div>
<input type="submit" name="submit" id="submit" value="Submit">

here is ajax

<script type="text/javascript" src="/js/jquery.1.7.2.min.js"></script>
<script type="text/javascript" src="/js/jquery.validate.min.js"></script>
<script type="text/javascript">
jQuery.validator.addMethod("mobunique",function(value){
  return eval($.ajax({
    url: 'abc.php',
    data: "mob="+value,
    type: 'post',
    async: false
  }).responseText);
}, 'Mobile No exists, must be unique!');

$(function() {
// probably here is issue, i dont declare form name any where )
   $('#form').validate();
});
</script>

here is abc.php which handle database for ajax

<?php 
$dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
$sth = $dbh->prepare("SELECT FROM person WHERE mob = ?");
$sth->execute(mysql_real_escape_string($_POST['mob']));
if ($sth->rowCount() > 0):
   return true;
else:
   return false;
endif;
?>

Here is backend

<?php
    // getting all values from the HTML form
    if(isset($_POST['submit']))
    {

        $idno = $_POST['idno'];
        $name = $_POST['name'];
  	$mob = $_POST['mob'];
	
    }

    // database details
    $host = "localhost";
    $username = "root";
    $password = "";
    $dbname = "hmis";

    // creating a connection
    $con = mysqli_connect($host, $username, $password, $dbname);

    // to ensure that the connection is made
    if (!$con)
    {
        die("Connection failed!" . mysqli_connect_error());
    }

    // using sql to create a data entry query
    $sql = "INSERT INTO person (idno,  name, mob)
 VALUES ('$idno', '$name', '$mob')"; 

   
  
    // send query to the database to add values and confirm if successful
    $rs = mysqli_query($con, $sql);
    if($rs)
    {
        echo "";
    }
  

    // close connection
    mysqli_close($con);

?>

You don’t seem to have posted the code for abc.php where you check in your database to see that the mobile number is unique. If you can post that, someone will be able to see why it’s not working.

It is probably a better approach to set the mob column in your database to be unique, and trap the error when you attempt to insert the new record. The problem with relying on a pre-check is that a user may add the mobile number between the time that your pre-check runs, and the time that your insert query runs. A simple try/catch around the insert query would trap the error thrown by the second insert.

I agree that it’s a better user-experience to stop it with an appropriate message beforehand, but that should be in addition rather than instead of setting it as unique.

If you know that you don’t declare the form name anywhere, and that you have to, why not add that in and see if it fixes the issue?

this is abc.php code

<?php 
$dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
$sth = $dbh->prepare("SELECT FROM person WHERE mob = ?");
$sth->execute(mysql_real_escape_string($_POST['mob']));
if ($sth->rowCount() > 0):
   return true;
else:
   return false;
endif;
?>

in simple words i want to stop duplicate entry ion the basis of mobile number

Not an answer to your question, but this extension was deprecated in PHP 5.5.0. Are you really using a version of PHP < 5.5.0?

1 Like

no, i am using php 7.4, and I deleted this line form my code

Sorry, I don’t know how I didn’t see that in your original post.

Is return the appropriate way to return a value to an Ajax call? I’ve always used echo for that.

Is the problem that it never calls abc.php, or that it does call the code but allows the duplicate anway?

No, it’s not. The Ajax can only see what was sent back to the request; you can echo, or you could header an http status code, or something like that, but return doesnt send anything to the browser request.

1 Like

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