How to Check UserName and Email Availability From Database using PHP and Ajax

In this tutorial, we will see a PHP script to implement username live check feature using AJAX, jQuery, and MySQL. Checking UserName and Email Address of the user on SignUp is good way to prevent duplicate content on database and several other problems.

PHP, jQuery and HTML Code

<?php

define('DB_SERVER', "localhost");
define('DB_USER', "root");
define('DB_PASS', "");
define('DB_DATABASE', "whatsappdwld_db");
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);


if( isset( $_POST['submit_form'] ) )
{
    $name = $_POST['username'];
    $email = $_POST['useremail'];
    $password = $_POST['userpass'];
    $insertdata=" INSERT INTO user (name,email,password) VALUES( '".$name."','".$email."','".$password."' ) ";
    mysqli_query($con,$insertdata);
}
?>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script type="text/javascript">

            function checkname()
            {
                var name = $('#UserName').val();

                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 =$('#UserEmail').val();

                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="" 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>
            <input type="submit" name="submit_form" value="Submit">
        </form>

    </body>
</html>

PHP Code (checkdata.php)

<?php

define('DB_SERVER', "localhost");
define('DB_USER', "root");
define('DB_PASS', "");
define('DB_DATABASE', "whatsappdwld_db");
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);


if (isset($_POST['user_name'])) {
    $name = $_POST['user_name'];
    $checkdata = "SELECT name FROM user WHERE name='".$name."' ";
    $query = mysqli_query($con,$checkdata);
    if (mysqli_num_rows($query) > 0) {
        echo "User Name Already Exist";
    } else {
        echo "OK";
    }
    exit();
}

if (isset($_POST['user_email'])) {
    $emailId = $_POST['user_email'];
    $checkdata = "SELECT email FROM user WHERE email='".$emailId."' ";
    $query = mysqli_query($con,$checkdata);
    if (mysqli_num_rows($query) > 0) {
        echo "Email Already Exist";
    } else {
        echo "OK";
    }
    exit();
}
?>

Leave a Reply

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