This is what I use for that purpose:
JAVASCRIPT:
$(document).ready(function() {
$("#username").blur(function() {
//remove all the class add the messagebox classes and start fading
$("#username_status").removeClass().addClass('username_status').text('Checking username').fadeIn("slow");
//verify entered username
$.post("path to php file",{ user_name:$(this).val() } ,function(data) {
// username not avaiable
if (data=='no') {
$("#username_status").fadeTo(200,0.1,function() {
$(this).html('Username in use').addClass('username_status_error').fadeTo(900,1);
});
// username available
}else if (data=='yes') {
$("#username_status").fadeTo(200,0.1,function() {
$(this).html('username available').addClass('username_status_ok').fadeTo(900,1);
});
// username field empty
}else if (data=='empty') {
$("#username_status").fadeTo(200,0.1,function() {
$(this).html('username field is empty').addClass('username_status_error').fadeTo(900,1);
});
// username check error
}else{
$("#username_status").fadeTo(200,0.1,function() {
$(this).html('fail to check username').addClass('username_status_error').fadeTo(900,1);
});
}
});
});
});
PHP FILE TO POST WITH AJAX:
<?php
require_once ('db_connect.php'); // include the database connection
if ($_POST['user_name']!="") {
// check if username is available from login table
$query = sprintf(" select * from login where username = '%s' ", mysql_real_escape_string($_POST['user_name']));
if (mysql_query($query)) {
if (mysql_num_rows(mysql_query($query))>0) {
echo "no";
}else{
echo "yes";
}
}else{
echo "error";
}
}else{
echo "empty";
}
?>
AND HTML FORM:
<form name="login-form" id="login-form" class="clients_form" method="post" action="form post link">
<ol>
<li>
<label for="login_usr" class="OF_name">Username:<span class="mandatory">*</span><br></label>
<input tabindex="1" accesskey="u" name="username" type="text" maxlength="30" id="username">
</li>
<li>
<label for="login_pwd" class="OF_email">Password:<span class="mandatory">*</span><br></label>
<input tabindex="2" accesskey="p" name="password" type="password" maxlength="15" id="password">
</li>
<li class="buttons">
<input class="btn" tabindex="3" accesskey="l" type="submit" name="cmdlogin" value="Login">
<input class="btn" name="cancel" type="button" value="Cancel" onclick="location.href='cancel link'">
</li>
</ol>
<br><br><br>
</form>
This is a simple sample that I've created to show you, but should be working fine...
When the user leaves the username
field, the AJAX will post the PHP file
to verify database, if the user
returns to the username field, again,
wend it leaves it, Ajax will post.
Hope this helps!