Checking availability for username in Signup form

hii guys,

you may noticed that in signup form username have check availability option. in so many website signup form when you type username and move your focus to another textfield at that time if that username is not available then one notification will come nearby username field.

so let’s code for it..

first create index.html file and write bellow code in that

index.html

<html>
<head>
<title>Signup</title>
</head>
<body>
<input type=”username” id=”username” name=”username” value=”” onBlur=”checkuname()”><span id=”username_status”> </span></td>
</body>
</html>

now, write bellow javascript code inside <script> tag.

$(document).ready(function()
{
$('#username').keyup(function()
{
var username = $(this).val();
$('#username_status').text('searching..');
if(username != "")
{
$.post('check availability.php',{username:username},function(data){
$('#username_status').text(data);
});
}
});
});

check availability.php

<?php
require ‘inc/connect.php’;
if(isset($_POST[‘username’]))
{
$u = $_POST[‘username’];
if(!empty($u))
{
$res = mysql_query(“select count(‘uname’) from signup where uname=’$u’”) or                                              die(mysql_error());
$ures = mysql_result($res,0);
if($ures == 0)
{
echo “Available”;
}
else
{
echo “Not Available”;
}
}
}
?>

so here, i used select query to search that username in my database table. if it is there in my table i will display “Not Available” message. but if it is not there in my table i will display “Available” message.

so simple.!

hope you like this.!!

Advertisements

2 thoughts on “Checking availability for username in Signup form

  1. Just two things to add:

    1. The obvious one: your code is vulnerable to SQL injection as there are no methods in place (escaping, prepared statements, etc).
    2. “COUNT(‘uname’)” is actually less efficient than “COUNT(*)” – as the * has a different meaning when used in conjunction with COUNT.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s