Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if multiple fields are in the database using ajax

How to check multiple field if fields are in the database and echo "OK" if not then "invalid"

Here is my the html code

<html>
<head>
<script src="Bootstrap/js/jquery.min.js"></script>
<script src="Bootstrap/js/bootstrap.min.js"></script>
<script src="Bootstrap/js/npm.js"></script>
<script type="text/javascript">

function checkname()
{
 var name=document.getElementById( "UserName" ).value;

 if(name)
 {
 $.ajax({
  type: 'post',
  url: 'checkdata.php',
  data: {
  user_name:name,
 },
  success: function (response) {
   $( '#name_status' ).html(response);
   if(response=="OK")   
   {
    return true;    
   }
    else
   {
    return false;   
   }
  }   
  });
 }
 else
 {
  $( '#name_status' ).html("");
  return false;
 }
}

function checkemail()
{
 var email=document.getElementById( "UserEmail" ).value;

 if(email)
 {
  $.ajax({
  type: 'post',
  url: 'checkdata.php',
  data: {
  user_email:email,
  },
  success: function (response) {
   $( '#email_status' ).html(response);
   if(response=="OK")   
   {
    return true;    
   }
   else
   {
    return false;   
   }
  }
  });
 }
 else
 {
  $( '#email_status' ).html("");
  return false;
 }
}

 function checkall()
{
 var namehtml=document.getElementById("name_status").innerHTML;
 var emailhtml=document.getElementById("email_status").innerHTML;

 if((namehtml && emailhtml)=='OK')
 {
 return true;
 }
 else
 {
 return false;


}
}

</script>
</head>
<body>

<form method="POST" action="insertdata.php" onsubmit="return checkall();">
 <input type="text" name="username" id="UserName" onkeyup="checkname();">
 <span id="name_status"></span>
 <br>
 <input type="text" name="useremail" id="UserEmail" onkeyup="checkemail();">
 <span id="email_status"></span>
 <br>
 <input type="password" name="userpass" id="UserPassword">
 <br>
 <button type="submit" name="submit_form" value="Submit">Submit</button>
</form>

</body>
</html>

here is the checkdata.php

<?php
$servername = "localhost";
$username = "root";
$password = "louchin";
$dbname = "demo";


$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
?>


<?php
if(isset($_POST['user_name']) && isset($_POST['user_email'])){
 $name=$_POST['user_name'];
 $emailId=$_POST['user_email']; 

 $checkdata="SELECT name,loginid FROM user WHERE name='$name' AND loginid='$emailId' ";

 $query=mysqli_query($conn, $checkdata);

 if(mysqli_num_rows($query) > 0)
 {
  echo "User Name Already Exist";
 }
 else
 {
  echo "OK";
 }
 exit();
}
?>

To be more clearer, I want to achieve is that the values of the name and email from the database is equals to the inputted value. And it will only display a single "OK". Just started using ajax. Please help. Ty

like image 610
sauce Avatar asked Jan 19 '26 02:01

sauce


2 Answers

As you are testing for data at the time of user input, you should check for username and email individually. For this your ajax function looks ok, but your php code should be modified.

//Your database code

<?php
if(isset($_POST['user_name']) && isset($_POST['user_email'])){
 //Check if username and email both is exist

 $name=$_POST['user_name']; 
 $emailId=$_POST['user_email']; 

 $checkdata="SELECT name,loginid FROM user WHERE name='$name' AND loginid='$emailId';";

 $query=mysqli_query($conn, $checkdata);

 if(mysqli_num_rows($query) > 0)
 {
  echo "User Name And Email Already Exist";
 }
 else
 {
  echo "OK";
 }
 exit();
} else if(isset($_POST['user_name'])){
 //Check if username is exist
 $name=$_POST['user_name']; 

 $checkdata="SELECT name,loginid FROM user WHERE name='$name';";

 $query=mysqli_query($conn, $checkdata);

 if(mysqli_num_rows($query) > 0)
 {
  echo "User Name Already Exist";
 }
 else
 {
  echo "OK";
 }
 exit();
} else if(isset($_POST['user_email'])){
 //Check if email is exist
 $emailId=$_POST['user_email']; 
 $checkdata="SELECT name,loginid FROM user WHERE loginid='$emailId';";

 $query=mysqli_query($conn, $checkdata);

 if(mysqli_num_rows($query) > 0)
 {
  echo "User Email Already Exist";
 }
 else
 {
  echo "OK";
 }
 exit();
} else {
  echo 'Not valid test data provided';
}
?>
like image 59
Prashant Avatar answered Jan 20 '26 15:01

Prashant


If the query you are using is correct, then use return instead of echo in the file checkdata.php

<?php
$servername = "localhost";
$username = "root";
$password = "louchin";
$dbname = "demo";


$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
?>


<?php
if(isset($_POST['user_name']) && isset($_POST['user_email'])){
 $name=$_POST['user_name'];
 $emailId=$_POST['user_email']; 

 $checkdata="SELECT name,loginid FROM user WHERE name='$name' AND loginid='$emailId' ";

 $query=mysqli_query($conn, $checkdata);

 if(mysqli_num_rows($query) > 0)
 {
  return "User Name Already Exist";
  exit();
 }
 else
 {
  return "OK";
  exit();
 }     
}
?>
like image 22
sujivasagam Avatar answered Jan 20 '26 15:01

sujivasagam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!