Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Username availability checking in database using php with jquery ajax method

Jquery ajax database username checker 
Jquery Ajax is the best way to check whether the username is available or not. Only using core ajax code is too long and difficult rather than ajax jquery. So use, jquery ajax method for ajax operation. Actually, ajax helps to get or retrieve and post the data from and into the database without reloading the pages.

Lets follow these core coding of jquery ajax which helps you to check the availability of username or any other data.

First of all you need different 3 codes:

  • Database table
  • Simple HTML Form with Jquery Ajax method
  • PHP coding "check_username.php"

Database Table

Create a table name users in database name test using like following codes:
CREATE TABLE users(
id int(11) not null auto_increment
username varchar(255) UNIQUE KEY,
password varchar(255),
PRIMARY KEY (id),
);

Simple HTML Form





ADD BELOW SCRIPT HERE OR ADD THE URL



username

password





JQUERY AJAX SCRIPT

$(document).ready(function(){
      $('#username').keyup(function(){
          var uname = $(this).val();
         
        if(uname.length>5)
          {
              $(#res).html("Processing...");
               $.ajax({
                    type:'post',
                    url:"check_username.php",
                    dataType:'text' ,
                     data:{username:uname}, //key value pairing
                              success:function(value){
                                 $('#res').html(value);
                              }
                  });
           }
       
            else{
                  $('#res').html('
Needs more than 5 characters
');
        }
     });
});

check_username.php PHP file

//create database connection first
$dbConn = new mysqli("localhost","root","password","test");
//check whether the connection is ready or not
if($dbConn->connect_error)
{
      die("Not connected with database, try again");
}
$username = $_POST['username'];
$sqlQuery = "SELECT USERNAME FROM users WHERE username = '$username' ";
$res = $dbConn->query($sqlQuery)
if($res->num_rows>0)
{
       echo "Username Already Taken";
}
else{
       echo "Username Available. Register Now";
}


I hope, the above code will help you to understand basic about .ajax() method of jquery. This is very much easy way to check username availability in database using jquery ajax method and php. 

If you have any difficulties to understand the above code, feel free to ask me. I will do my best to help you. Keep coding, Thank you.


This post first appeared on All Blog Solution, please read the originial post: here

Share the post

Username availability checking in database using php with jquery ajax method

×

Subscribe to All Blog Solution

Get updates delivered right to your inbox!

Thank you for your subscription

×