Create Simple Username Availability Checker Using jQuery & PHP

Read {count} times since 2020

Many sites have signup pages. Every programmers try to make the signup pages attractive by adding stylish input CSS codes and others. Why not add a stylish username availability checker ? It’s very easy to add and will work on jQuery versions 1.2 and up. I’m also going to tell you how to check if username is available in the server side. Of course it’s PHP. The checking is SQL Injection free too. For Executing SQL queries we will use PHP PDO.

First of all create two files index.php (signup page), check.php & checker.js
It’s not necessary to set the name of signup page to index.php. Any name will work.


  <script src=""></script>
  <script src="checker.js"></script>
  <input type="text" size="35" id="user" value="" placeholder="Type Your Desired Username"/>
  <input type="button" id="check" style="cursor:pointer;" value="Check Availablity"/><br/>
  <div style="margin:5px 15px;" id="msg"><b id="prev">subinsiby</b></div>

checker.js – Request To Check Username

  v.css({background:"url(../cdn/load.gif) no-repeat",backgroundSize: "2em",backgroundPosition: "center right"});
    $("#msg").html("<span style='color:green;'>The username <b>"+v.val().toLowerCase()+"</b> is available</span>");
    $("#msg").html("<span style='color:red;'>The username <b>"+v.val().toLowerCase()+"</b> is not available</span>");

check.php – Checks Username

if(isset($_POST) && $user!=''){
 $sql=$dbh->prepare("SELECT username FROM users WHERE username=?");
  echo "available";
  echo "not-available";

and of course config.php 

$host = "localhost"; // Database Hostname
$port = "3306"; / /MySQL Port : Default : 3306
$user = "username"; // Databse Username Here
$pass = "password"; // Databse Password Here
$db   = "database_name"; // Database Name
$dbh  = new PDO('mysql:dbname='.$db.';host='.$host.';port='.$port,$user,$pass);

When the user clicks the Check Availability button, jQuery will send a POST request to check.php with user parameter containing the username value the client entered. check.php will check it’s database with the username given by AJAX call. If it matches a user response will be not-available and if it doesn’t match a user, then the response will be available.
jQuery will make changes to the page according to the AJAX response ie jQuery will take care of the rest. If you have any suggestions / feedback/ problems , say it in the comments.

Show Comments