Username Validation Using Ajax


  • Here this is the program for username validation in PHP.
  • If user entered name which is already exists then it will display error message.
  • Here is the code for that program.
Clcik here to download program with source and databse


Tages : : email already exist validation in ajax laravel,check username availability using ajax and javascript php,username validation in jquery,check email availability using ajax php,username already exists in php using ajax,username already exists validation in jquery,django ajax check username,how to check if username already exists in database using jquery

<html>



<script language="javascript">
//Ajax Code
function inputcheck(str)
{
if(str=="")
{
document.getElementById("inputcheck").innerHTML=="";
return;
 }
if(window.XMLHttpRequest)
 {
    xmlhttp= new XMLHttpRequest();
 }
else
{
   xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
 xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("inputcheck").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","logcheck.php?check="+str,true);
xmlhttp.send();
}
//Fiished 
</script>
<body>
Enter UserName
<input type="text" name="txtuser" onblur="inputcheck(this.value)" />
<span id="inputcheck"></span>
</body>
</html>


<?php
      
?>
The Database validation File Code. This code check the table for username which is entered by user. if username already exist it will display error.
<?php
       
$re=$_GET['check'];
$con=mysql_connect("localhost","root","");
mysql_select_db("demo12");   
$qu="select * from demo where username='$re'";
$res=mysql_query($qu);
if(mysql_num_rows($res)==1)
{
echo '<p align=justify>Alredy Exits<p>';
}
else 
{
 echo 'Thank You';
}
?>

2 Comments

  1. If you want to develop new PHP project, I suggest you using MySQLi, because your php code soon will be deprecated. You can perform something like this:

    $db = new Db();
    $sql = $db->query("select * from demo where username='$re'");

    if($sql->num_rows) {
    echo '<p align=justify>Alredy Exits<p>';
    }
    else {
    echo 'Thank You';
    }

    ReplyDelete
    Replies
    1. Yes from now our all code will be on mysqli.

      Thanks.

      Delete
Previous Post Next Post