index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Fetch Information from DB Using Ajax</title>
<link rel="stylesheet" href="sty.css" type="text/css" />
<script>
function show(str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","list.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body id="b">
<div id="con">
<form name="form1" method="get" action="">
<h1>Fetch Information From DB Using Ajax</h1>
<div id="m">
<select name="selection" onchange="show(this.value)">
<option value="">-Select-</option>
<?php
$cnt=mysql_connect("localhost","root","");
$select_db=mysql_select_db("show_db",$cnt);
$select=mysql_query("select * from show_tbl",$cnt);
while($r=mysql_fetch_array($select))
{
?>
<option value="<?php echo $r['id'];?>">
<?php echo $r['companyname'];?></option>
<?php
}
?>
</select>
</div>
<br />
<div id="txtHint" style="color:#99F;font-size:18px; margin-left:30px;">Customer info will be listed here...</div>
</form>
</div>
</body>
</html>
sty.css
#b
{
background:#FFF;
}
#con
{
background:#666;
width:50%;
height:300px;
border:1px;
border-radius:30px;
margin:5% auto;
}
h1{font-style:italic;
color:#9CF;
text-align:center;
line-height:3;
}
#m
{
margin:5% auto;
margin-left:200px;
}
list.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root','');
mysql_select_db("show_db", $con);
$sql="SELECT * FROM show_tbl WHERE id = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1' >
<tr>
<th>Company Name</th>
<th>Contact Name</th>
<th>Address</th>
<th>City</th>
<th>Phno</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['companyname'] . "</td>";
echo "<td>" . $row['contactname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['phno'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
No comments:
Post a Comment