1

Check Username Availability With jQuery In PHP And MySql

This is a script to Check Username availability with Jquery in PHP and MySql, after entering the username it will check in the database and displays a message depending on the result from the database, we are using simple jquery code to achieve this, first of all using jquery, we are reading the input value and then sending it a php page, it will search the database table, depending on the response it will display message.

Code for Check Username availability with Jquery in PHP and MySql

First add the jqery file in the head section of html file

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>

After that your input element should have this code surrounded.

<span>
	<input name="username" type="text"id="username" />
	</span>	<span id="usernameLoading"><img src="indicator.gif" alt="Ajax Indicator" /></span>
<span id="usernameResult"></span> <input type="hidden" name="chkavailibility" value="0">

This is the Jquery code

<script type="text/javascript">

$(document).ready(function() {
	$('#usernameLoading').hide();
	$('#username').blur(function(){
	  $('#usernameLoading').show();
      $.post("checkAvail.php", {
        username: $('#username').val()
      }, function(response){
        $('#usernameResult').fadeOut();
        setTimeout("finishAjax('usernameResult', '"+escape(response)+"')", 400);
      });
    	return false;
	});
});

function finishAjax(id, response) {
  $('#usernameLoading').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
} //finishAjax
</script>

This is the checkAvail.php file

<?php
include "connection.php";
   $username = $_POST['username']; // get the username
if ($username=="")
 {
 // if the post value is empty it will not do anything
 }
 else
 { // if it's not empty, it will  query the database and outputs the message
    $query = "SELECT * FROM register WHERE uname ='$username'";
$result = mysql_query($query);
	$num_rows = mysql_num_rows($result);
	if($num_rows >0 )
	{
		echo 'Username Unavailable';
	}
	else
	{
		echo 'Username Available';
	}
}
?>

Incoming search terms:

  • check username availability in php and mysql
  • check username availability php
  • php check username function
Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 1 comments
Abhijt

thanks a lot I need this code

Reply

Leave a Reply:

x

Join My Facebook Group to Receive Coding Tutorial for Free

email-subsribe