sauce
sauce

Reputation: 345

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

Upvotes: 3

Views: 1313

Answers (5)

Prashant
Prashant

Reputation: 164

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';
}
?>

Upvotes: 3

sujivasagam
sujivasagam

Reputation: 1769

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();
 }     
}
?>

Upvotes: 2

Pravin Vavadiya
Pravin Vavadiya

Reputation: 3207

In your checkdata.php.

You get the value of Username and UserEmail like this

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

You can try something like this.

$name=$_REQUEST['username'];
$emailId=$_REQUEST['useremail'];

Upvotes: 0

B. Desai
B. Desai

Reputation: 16436

change your form and script as below: Only check both fields at one time at time of submit

<script type="text/javascript">


 function checkall()
{
  var name=document.getElementById( "UserName" ).value;
  var email=document.getElementById( "UserEmail" ).value;
  if(name)
 {
  if(email)
  {
    $.ajax({
    type: 'post',
    url: 'checkdata.php',
    data: {
    user_email:email,user_name:name
    },
    success: function (response) {
     $( '#email_status' ).html(response);
     $( '#name_status' ).html(response);
     if(response=="OK")   
     {
      return true;    
     }
     else
     {
      return false;   
     }
    }
    });

  }
  else
  {
    $( '#email_status' ).html("");
    return false;

 }
 else
 {
  $( '#name_status' ).html("");
  return false;
 }


</script>
</head>
<body>

<form method="POST" action="insertdata.php" onsubmit="return checkall();">
 <input type="text" name="username" id="UserName" >
 <span id="name_status"></span>
 <br>
 <input type="text" name="useremail" id="UserEmail" >
 <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>

Upvotes: 1

Barath
Barath

Reputation: 21

My insufficient reputation caused me to put this is in answers rather than a comment. In checkdata.php, you are expecting both user_name and user_email. But the ajax calls contains either user_name or user_email. You can either send both or fork the checkdata.php to check them separately.

Upvotes: 0

Related Questions