Monday, July 21, 2008

AJAX Username Availability checking

Put this javascript within your "Head" tag.


the form control should like this,

call the CheckUsername() function inside the username control in onchange event,
onChange='CheckUsername(this.value);'
create one "div" tag put inside that tag
id='usernameresult'

create one check.php file, it should consists,

mysql_connect("localhost","root","1234");
mysql_select_db("database_name");

if(isset($_GET['username'])){ // ElseIf they are want to check their username.

$user=stripslashes(strip_tags(htmlspecialchars($_GET['username'], ENT_QUOTES))); // Cleans all nasty input from the username.
$check=mysql_num_rows(mysql_query("SELECT * FROM `registration` WHERE `Usern` = '".$user."'")); // Checks to see if a user is in the `users` table or not.

if($check == 0){ // If there is no username in the database that matches the input one.
echo ''.$user.' is Available!'; // Yay we can use it.
}elseif($check != 0){ // ElseIf there is a username in the database.
echo ''.$user.' is Not Available!'; // None for you higgans.
} // End If.

No comments: