Friday, 7 March 2014

Retrive Data From Database without refreshing the page using AJAX in PHP

To Fetch Data From the Database without refreshing the whole page using AJAX we have to create two file's that named as..


  • getuser.php
  • test.html
so first let's start the coading of test.html


<html>
<head>
<script type="text/javascript">
<!--
var xmlhttp;
function showuser(str)
{
if(window.XMLHttpRequest)
{
xmlhttp= new XMLHttpRequest();
}
if(window.ActiveXObject)
{
xmlhttp= new ActiveXobject("Microsoft.XMLHTTP");
}
var url="getuser.php?q="+str;
xmlhttp.onreadystatechange=statechanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function statechanged()
{
if(xmlhttp.readyState==4)
{
document.getElementById("txthint").innerHTML=xmlhttp.responseText;
}
}
-->
</script>
</head>
<body>
<form name="frm">
SELECT A PERSON : 
<select name="s" onChange="showuser(this.value)">
<option value="1">Nilesh</option>
<option value="2">Sagar</option>
<option value="3">Jaynit</option>
<option value="4">Ravi</option>
<option value="5">Chirag</option>
</select>
<input type="text" onKeyUp="showuser(this.value)">
<div id="txthint">YOUR DATA DISPLAY HERE.....</div>
</form>
</body>
</html>


now we create a getuser.php file



<?php
$q=$_GET['q'];
$link=mysql_connect('localhost','root','');
mysql_select_db("student");
$result=mysql_query("select * from student where name like '".$q."%'");
echo "<table border=1>
<tr>
<th>ID</th>
<th>Name</th>
<th>City</th>
</tr>";
while($rs=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$rs[0]."</td>";
echo "<td>".$rs[1]."</td>";
echo "<td>".$rs[2]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($link);
?>


Now you have to create one database in MySql named as "student"
and create one table also name as "student"

The whole package you can Download From Here

No comments:

Post a Comment