9lessons programming blog
Loading Search
Thursday, December 17, 2009

Live Availability Checking with jQuery.

I received a mail from my reader that asked to me how to implement live username availability checking. So I had prepared a simple and understanding tutorial with changing background color using jQuery and Ajax. Take a look at live demo.

Live Availability Checking with jQuery.

Download Script     Live Demo

jQuery 1.3 with PHP

user_check.php
$('#username').change(function(){} - username is the ID of the input. Using $("#username").val("id") calling input field value. First checking the value string length max 3 (username.length > 3))

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js
"></script>
<script type="text/javascript">
$(document).ready(function()
{

$("#username").change(function()
{
var username = $("#username").val();
var msgbox = $("#status");

if(username.length > 3)
{
$("#status").html('<img src="loader.gif">&nbsp;Checking availability.');

$.ajax({
type: "POST",
url: "check_ajax.php",
data: "username="+ username,
success: function(msg){
$("#status").ajaxComplete(function(event, request){

if(msg == 'OK')
{
// if you don't want background color remove these following two lines
$("#username").removeClass("red"); // remove red color
$("#username").addClass("green"); // add green color
msgbox.html('<img src="yes.png"> <font color="Green"> Available </font>');
}
else
{
// if you don't want background color remove these following two lines
$("#username").removeClass("green"); // remove green color
$("#username").addClass("red"); // add red  color
msgbox.html(msg);
}
});
}
});

}
else
{
// if you don't want background color remove this following line
$("#username").addClass("red"); // add red color
$("#status").html('<font color="#cc0000">Enter valid User Name</font>');
}
return false;
});
});
</script>
<input type="text" name="username" id="username" />
<span id="status"></span>

check_ajax.php
Contains PHP code.
<?php
include('db.php');
if(isSet($_POST['username']))
{
$username = $_POST['username'];
$username = mysql_real_escape_string($username);
$sql_check = mysql_query("SELECT user_id FROM users WHERE username='$username'");
if(mysql_num_rows($sql_check))
{
echo '<font color="#cc0000"><b>'.$username.'</b> is already in use.</font>';
}
else
{
echo 'OK';
}
}
?>



CSS Code
body
{
font-family:Arial, Helvetica, sans-serif
}
#status
{
font-size:11px;
margin-left:10px;
}
.green
{
background-color:#CEFFCE;
}
.red
{
background-color:#FFD9D9;
}

Sponsored Links

Share this post

Comments
{ 30 comments }
Anonymous said...

Just love`it!

Ravi Kumar Tamada said...

ThanQ !

Ibrahim Azhar Armar said...

LI love the every effort you put on to expalin, really awesome and useful post,.. thankk u... :)

EL REY said...

thanks so much for this tutorial, I have a different question; How can I get a comment box like this? if you could answer that would be great thanks again

Anonymous said...

Just great, can put timer on image loader by example 3 seconds 2 seconds qherever i want ??

Dwarakesh said...

can u use timer in the above code... me struggling .. Plz help

diknowstech said...

Nice Post,
Thank you.

Anonymous said...

great post

Motyar said...

Useful

Anonymous said...

Nice

Anonymous said...

:D THX

Anonymous said...

USEFUL AND NICE.
Thanks.

Anonymous said...

A very nice script, though if you try one and its not available and you try another after the loading animation is finished it will show the previous result for a short second. Just thought I would mention it :).
A script I will for sure use!

dmikester1 said...

This is awesome! Exactly what I've been looking for for my forms. Well done!

Anonymous said...

thank you ,perfect work

aleks said...

very nice! thanks for sharing!

shrikant gupta said...

awesome work man

Anonymous said...

hey nice code..use comments so that code will be more understandable..

Charlie said...

Thanks Srinivas!

Following you on twitter now, this is probably the best page on the net that shows how to use jQuery and other great techniques!

Best Regards

Charlie

Anonymous said...

Just a tip!

You´ve made a variable stating that status is $("#status) div.

Use that in all places instead of the:

$("#status").html( mg src="loader.gif"> Checking availability.');

like-> status.html('img....'>" Checking availability.');

Thank you for the best learningsite on the web!

Anonymous said...

nice post

Ankul Gupta said...

liked it thanx

Zain Baloch said...

Awesome bro, it saved me a life time..
thank you for sharing..

Anonymous said...

Not too friendly for someone without too much experience :-(

Could not get it to work.

Too complicated....not enough explanation of where things go.

But I'm sure if you're already a web developer with LOT and LOTS of knowledge it's a breeze...so thanks !

martyyy said...

thank you for every lesson in your website

Anonymous said...

great

Anonymous said...

Goooooooooooooooood!

Sanat Verma said...

mysql_num_rows(): supplied argument is not a valid MySQL result resource in E:\xampp\htdocs\live_availability\check_ajax.php on line 12

abhishek said...

hello... i want to use ur code with jsp.... please tell me how it would be done... i tried it and its working fine in php... i changed the file name to jsp where it is calling the php file(check_ajax.php) but its not working.... please tell me as soon as possible....

Anonymous said...

sir , i need your help .may i

Post a Comment

Subscribe now!Recent Posts

Categories

Subscribe now!Popular Posts

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Like Me

follow me
products

9lessons labs

9lessons clouds

Android application

Chrome Extension