DEVELOPMENT

Signing up for the websites surely becomes frustating when you find that the username of your choices are not available. However, the code i've written here to check the availability of username is not frustrating at all.

THE SITUATION

username av or nt


INDEX.PHP
<!DOCTYPE html>
<html>
<head>
<script>
function ucheck(str) {
var xmlhttp;
if (window.XMLHttpRequest)
 {
 xmlhttp=new XMLHttpRequest();
 }
else
 {
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.onreadystatechange=function()
 {
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
 {
 document.getElementById("chck").innerHTML=xmlhttp.responseText;
 }
 }
xmlhttp.open("POST","chck.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("uname="+str);

 }
</script></head>
<body>
<form >
Username : <input type="numeric" id="name" onblur="ucheck(this.value)">
</form> 
<div id="chck" style="display: inline-block"></div>
</body>
</html>
    

THE DATABASE

Pop-up div

CHCK.PHP
<link rel="stylesheet" href="style.css" type="text/css" />
<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
{

$name=$_POST['uname'];
$c=0;
$conn=@mysql_connect("localhost","root","") or die("could not connect to database");
$db=@mysql_select_db("blog",$conn) or die("could not select database");
$sql="SELECT * FROM `check`";
$result = mysql_query($sql,$conn)or die("Error");

while($row = mysql_fetch_array($result)) 
{
 if($row['uname']==$name)
 {
 $c=1;
 }
}
if($c==0)
{
 echo "<span class=\"av\">Available</span>";
 
}
else
{
 echo "<span class=\"unav\">Username is already taken, Please try something else</span>";
 
}
 
}

?>

STYLE.CSS
.av
{ 
     color:green;
     border: 1px solid green;    
}
.unav
{
    color:red;
    border: 1px solid red;    
}
    


Leave a Comment  
Download Code(62)





All rights reserved, the content is copyrighted to the author.

RELATED POST