Check Email Already Exists Using JQuery Ajax PHP MySql

Check Email Already Exists Using JQuery Ajax PHP MySql

Here we will show how can live check email exists and not using Ajax, JQuery , PHP and MySql. This is a common feature in most of website available online.

The PHP script matches the user input against the database and returns the reponse.

Below is a sample table.

				
					--
-- Table structure for table `user_info`
--
CREATE TABLE `user_info` (
  `id` int(11) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `email` varchar(255) NOT NULL,
  `age` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `user_info`
--
INSERT INTO `user_info` (`id`, `name`, `email`, `age`) VALUES
(1, 'Amit', 'amit@gmail.com', 20),
(2, 'Anil', 'anil@hotmail.com', 21),
(3, 'Mahesh', 'mahesh@gmail.com', 20),
(4, 'Arvind', 'arvind@outlook.com', 24),
(5, 'Amit', 'amit@gmail.com', 20),
(6, 'Vedant', 'vedant@yahoo.com', 21),
(7, 'Rajendra', 'rajendra@outlook.com', 23),
(8, 'Arvind', 'arvind@outlook.com', 24),
(9, 'Kuldeep', 'kuldeep@outlook.com', 19);
--
-- Indexes for table `user_info`
--
ALTER TABLE `user_info`
  ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for table `user_info`
--
ALTER TABLE `user_info`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;
COMMIT;
				
			
Now Create connection.php file to connect with database. Below is code.
				
					<?php
  error_reporting(0);
  $mysql_hostname = "localhost";
  $mysql_user     = "root";
  $mysql_password = "";
  $mysql_database = "DATABASE_NAME"; # Here insert your Database name
  $bd = ($GLOBALS["___mysqli_ston"] = mysqli_connect($mysql_hostname,  $mysql_user,  $mysql_password)) or die("Opps some thing went wrong");
  mysqli_select_db( $bd, $mysql_database) or die("wrong");
?>
				
			
Now create index.php file contains email input field.Below is code
				
					<!DOCTYPE html>
<html>
  <head>
    <title>Inflay.com | Check Email Already Exists Using JQuery Ajax PHP MySql</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  </head>
  <body>
    <div class="container">
      <h3>Check Email Already Exists Using JQuery Ajax PHP MySql</h3>
      <br />
      <form action="" method="post">
        <div class = "row">  
          <div class="col-md-3">
            <label for="country">Email</label>
              <input type = "email" class="form-control" id="email" name="email" value = "">
              <lable id="email_msg"></lable>
          </div>
        </div>
      </form>
    </div>
  </body>
</html>
<style type="text/css">
  .red{
    color: red;
  }
  .green{
    color: green;
  }
</style>
				
			
Below is Javascript code triggers Ajax call blur event of email input. $.ajax send email entered by the user along with the request for a PHP page.
				
					<script type="text/javascript">
  $(document).ready(function(){    
    $('#email').blur(function(){  
      var input = $(this).val();  
      if(input != '')  
      {  
        $.ajax({  
                url:"check_input.php",  
                method:"POST",  
                data:{input:input},  
                success:function(data)  
                {  
                  $('#email_msg').html(data);  
                }  
              });  
      }  
    });  
  });  
</script>

				
			

Match Email against Database using PHP

Now Create check_input.php file executed as a result of the jQuery AJAX call. It compares database and user data to check email. Based on the availability status it will respond to the AJAX call.

Below is code.

				
					<?php include_once('connection.php');

  if (isset($_POST['input']) && !empty($_POST['input'])) 
  {
    $query = "SELECT id FROM user_info WHERE email = '".$_POST["input"]."'";
    $result  = mysqli_query($GLOBALS["___mysqli_ston"], $query);
    $num_result = mysqli_num_rows($result);    
    if ($num_result > 0) 
    {
      $result = "<span class = 'red'> Email Not Available.</span>";
    }
    else
    {
      $result = "<span class = 'green'> Email Available.</span>";  
    }       
    echo $result;
  } 

?> 
    
				
			

OUTPUT

I hope this article helps you.
Thanks for visiting Inflay.com .