How to Check username & email already exists using jquery ajax in PHP mysql

In this post you will learn how to check username & email is already exists in database using jquery AJAX in PHP mysql.So, when you enter your username or email, at this time the Ajax function, called the server, is the username or email that is entered unique or not.

The database is checked on the server side and determined whether the username or email entered is available or not. Then it will return to the Ajax request for the username and email or not and the results will be displayed immediately without refreshing the page.

Create a Database in MySQL

The first step in this tutorial is the creation of a MySQL database. you can create tables by running SQL queries. Create a table ‘users’ in database using the following SQL query.

-- Database: `testing`
-- --------------------------------------------------------
-- Table structure for table `users`
CREATE TABLE `users` (
  `name` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `username` varchar(50) NOT NULL,
  `age` varchar(50) NOT NULL,
  `city_name` varchar(50) NOT NULL

Create MySql Connection in PHP

For importing and exporting database in MySql will make a separate file ‘dbCongif.php’. Add the following code and replace the database credentials with yours. You can find your db credentials in Application Access details:


	// Database configuration 
	$dbHost     = "localhost"; 
	$dbUsername = "root"; 
	$dbPassword = ""; 
	$dbName     = "testing"; 
	// Create database connection 
	$con = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName); 
	// Check connection 
	if ($con->connect_error) { 
	    die("Connection failed: " . $con->connect_error); 

Create HTML Form Page


<!Doctype html>
<html lang="en">
  <meta charset="utf-8">
  <title>Check username & email already exists using jquery ajax in php mysql</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>
  <body style="background-color: #ebebeb">
    <div class="container" style="margin-top: 50px;">
      <h2 class="text-center">Check username & email already exists using jquery ajax in php mysql</h2>
      <div class="row">
        <div class="col-md-3"></div>  
        <div class="col-md-6" style="margin-top:20px; margin-bottom:20px;">
          <form id="formSubmit">
            <div class="form-group">
              <input type="text" class="form-control" id="username" placeholder="Username">
              <span id="message" style="color:#ff1a1a"></span>
            <div class="form-group">
              <input type="text" class="form-control" id="email" placeholder="Email">
              <span id="message" style="color:#ff1a1a"></span>
            <button type="submit" class="btn btn-success">Submit</button>

Create jquery AJAX Script

<!--- Check username exists using jquery ajax --->
<script type="text/javascript">
        var username = $("#username").val();
        var email = $("#email").val();
      if (username !=="" && email !== "") {
              url : "checkUsername.php",
              type : "POST",
              data : {username:username,email:email},
                if (result == 1) {
                  $("#message").text('Sorry... username or Email is already exists');
                  $("#message").text('Username saved successfully');
          $("#message").text('Please fill the all fields');

Create PHP page to check username or email availability 


    // Include database connection
    include_once "dbConfig.php";
    // Check username is already exists in database

    $username = $_POST['username'];
    $email = $_POST['email']; 
    $query = "SELECT * FROM users WHERE username = '$username' || email = '$email'";
    $result = $con->query($query);
    if ($result->num_rows > 0) {
        echo 1;
      $query = "INSERT INTO users ('name,email,username,age,city_name') 
                VALUES('john cena',$username','$email',35,'colifornia')";
      $result = $con->query($query);
      echo 0;


How to Check username & email already exists using jquery ajax in PHP mysql

You can always support by sharing on social media or recommending my blog to your friends and colleagues. If you have any suggestions / problems about this tutorial, please comment on the  form below.😊

Leave a Reply

Your email address will not be published. Required fields are marked *