JavaScript

PHP

Welcome Programmers, 
Get your PHP Code & Save your time

Script For Checking user-name availability


     The Internet is commonly asking for more and more user names for individual websites. A username is commonly paired with a password, but the username is not private. Sometimes, it can be difficult to find a username that is not taken. If you want to allow user to check username entered by him/her is available for your site then this script will be useful to you.
      Number of SocialNetworking sites & other web-sites uses this feature for their user registration process.
Ex.





How To Make It Work
It is very simple to make this script work. You need two files, a HTML file (say, validate.html) and a PHP file (say, validate.php) to interact with the database and check for the availability of the required username. Keep both files in same folder for making it to work correctly., otherwise if you want to keep the PHP file somewhere else, then you can specify the absolute path.
Now call the function which i have mention below on 'OnBlurEvent ' of username field that you have created using html. You can plave this function in separate JS file to keep code clear. This is an Ajax based function.

var xmlHttp;
function GetXmlHttpObject()

{ // This function we will use to call our xmlhttpobject.
var objXMLHttp=null // Sets objXMLHttp to null as default.
if (window.XMLHttpRequest)
{ // If we are using Netscape or any other browser than IE lets use xmlhttp.
objXMLHttp=new XMLHttpRequest() // Creates a xmlhttp request.
}
else if (window.ActiveXObject)
{ // ElseIf we are using IE lets use Active X.

objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") // Creates a new Active X Object.
} // End ElseIf.
return objXMLHttp // Returns the xhttp object.
}
function checkuser()
{
var uname=document.getElementById("username").value;
if(uname!="")
{
xmlHttp=GetXmlHttpObject() // Creates a new Xmlhttp object.
if (xmlHttp==null)
{ // If it cannot create a new Xmlhttp object.
alert ("Browser does not support HTTP Request") // Alert Them!
return // Returns.
} // End
var url="useravailability.php?unm="+uname;
xmlHttp.open("GET",url,false);
xmlHttp.send();
if(xmlHttp.responseText!="")

{
document.getElementById("uname").innerHTML = xmlHttp.responseText;
return false;
}
}
}

Now Create PHP page that contain following code.

include "config.php"; //your configuration file to db...
$uname=$_GET['unm'];
$userqry=mysql_query("SELECT * FROM user where username='$uname'");

if(mysql_num_rows($userqry)!="0")
{
echo 'username is not availble';         //      put msg in span
}
else
{
echo 'username is availble';

}