pTutorial

New way to learn Programming

Check user availability using ajax

Following Ajax example with PHP help you to check user availability. We use Ajax to send partial request with PHP.

You can check this example by typing any name of the country like India, Japan, Ireland etc.

Name of Database

test

Name of table

countries

Query

select *from countries where value='$a'"

Ajax user availability Example

<html>
<head>
<script type="text/javascript">
function check()
{
var xmlhttp;
var k=document.getElementById("name").value;
var url="php-connect.php?val="+k;
 
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4)
    {
        document.getElementById("uk").innerHTML=xmlhttp.responseText;
 
    }
  }
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
</head>
<body>
 
Enter name: <input type="text" name="name" id="name" onKeyUp="check()">
<span id="uk"> </span>
 
</body>
</html>

PHP FILE

<?php
$e=mysql_connect("localhost","root","") or die("con fail");
$f=mysql_select_db("test",$e) or die("cont fail 2");
$a=$_GET['val'];
$qc="select *from countries where value='$a'";
$data1 = mysql_query($qc)or die(mysql_error());
while($row = mysql_fetch_array($data1)) 
{
if(mysql_num_rows($data1)>0)
{
    echo "<font color=red?>";
    echo "country already exist";
    echo "</font?>";
}

}
?>

Download and execute instruction

First you have to download this example and after that create a database named "test" and import the uk.sql (provided in the download folder) file into the test Database and execute this example.

See Also


QUOTES OF THE DAY

Impossible is a word only be found in the dictionary of fools.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER