3

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

3
  • is my checkdata.php is not sending one request? Commented Mar 7, 2017 at 4:39
  • @guradio don't mind the form where to submit, I've just want when I type in the name and the email it will display ok if it's in the database. Commented Mar 7, 2017 at 4:45
  • This condition is not satisfying your code if(isset($_POST['user_name']) && isset($_POST['user_email'])). Here specify user_name and user_email is set in your POST array. But you done it with two separate ajax calls. Please write two ajax calls with single one Commented Mar 7, 2017 at 4:53

5 Answers 5

3

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';
}
?>
Sign up to request clarification or add additional context in comments.

5 Comments

Yes it's working but the problem with this is the query, that's why If possible to combine the two $_post. Because my query should be SELECT name,loginid FROM user WHERE name='$name' AND loginid='$emailId'
In that case you need both username and email for testing, and this data not available first when user is typing username (so your keyup function will not return anything as email is missing at this stage). So if you want to test both combined then you should put ajax in checkall() function as suggested by B. Desai
That code should work, i have checked it and it seems it is missing two closing curly brackets, you should verify it and correct it then try again. Also check for any JavaScript error in console for batter understanding.
I've added the curly brackets and still can't make it work
I have modified my PHP code so it should test three different conditions based on data provided, please try using this new code.
2

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

1 Comment

mmm. it still does not display the return
1

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>

1 Comment

did not do anything :(
0

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.

2 Comments

I want them combine because I'm selecting a row. as you can see in my query.
You can have single checkall() function just like the above answer by B.Desai, in which you send and check them together.
0

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'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.