Home  • Programming • JavaScript

Check user availability by AJAX application

ajax-check-availability user.sql
create table user(
 id int(10) primary key auto_increment,
 username varchar(20) unique not null,
 password varchar(30)
);

insert into user(username,password)values('rahim','11111');
insert into user(username,password)values('karim','22222');
insert into user(username,password)values('kamal','33333');
usercheck.php
<?php
  if(isset($_GET["username"])){
      $username=$_GET["username"];

      $db=new mysqli("localhost","root","","test");
      $rs=$db->query("select count(*) from user where username='$username'");
      list($count)=$rs->fetch_row();
      if($count){

        echo "Not Available";
      }else{
        echo "Available";
      }

  }
?>
app.php
<html>
<head>
 <title>AJAX Example</title>
<script>
function ajaxGET(div,url){
	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(div).innerHTML=xmlhttp.responseText;
	  }else{
document.getElementById(div).innerHTML="Checking...";
          }
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
}
</script>
</head>
<body>
<form name='frmStu'>
Username:<input type='text' name='txtName' onblur="ajaxGET('output','usercheck.php?username='+this.value)" /><span id='output'></span><br/>
Password<input type='password' name='txtPass' /><br/>
<input type='button' value='Show' onclick="alert('Submitted')" />
</form>

</body>
</html>

Comments 8


Copyright © 2026. Powered by Intellect Software Ltd