Display data which are related to selected combo box value.Data will be changed when the combo box value gets changed.
sql
</pre>
CREATE TABLE IF NOT EXISTS `ajax_database` (
`id` int(32) NOT NULL,
`name` text NOT NULL,
`address` text NOT NULL,
`age` int(32) NOT NULL,
`city` varchar(30) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `ajax_database`
--
INSERT INTO `ajax_database` (`id`, `name`, `address`, `age`, `city`) VALUES
(1, 'Nimesha', 'kelaniya,sri Lanka', 26, 'Kelaniya'),
(2, 'Savinda', 'Polhena,Sri Lanka', 17, 'Kelaniya'),
(3, 'John', 'Canada', 20, 'abc');
<pre>
index.php
<html>
<head>
<script type="text/javascript">
function showUser(str)
{
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)
{
var myTable = document.getElementById('tblDisplay');
var tBody = myTable.getElementsByTagName('tbody')[0];
tBody.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax.php?id="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form onLoad="getusers()">
<select name="users" onchange="showUser(this.value)">
<option value=''>Select</option>;
<?php
$con = mysql_connect('localhost', 'root', 'adminpns');
mysql_select_db("site", $con);
$sql="SELECT * FROM ajax_database";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo "<option value='$row[id]'>$row[name]</option>";
}
?>
</select>
<table border='0' id="tblDisplay">
<tbody>
</tbody>
</table>
</form>
<br />
</body>
</html>
ajax.php
<?php
$id=$_GET["id"];
$con = mysql_connect('localhost', 'root', 'adminpns');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("site", $con);
$sql="SELECT * FROM ajax_database WHERE id = '".$id."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo "<tr><td>ID :</td><td>" . $row['id'] . "</td></tr>";
echo "<tr><td>Name :</td><td>" . $row['name'] . "</td></tr>";
echo "<tr><td>Address :</td><td>" . $row['address'] . "</td></tr>";
echo "<tr><td>Age :</td><td>" . $row['age'] . "</td></tr>";
echo "<tr><td>City :</td><td>" . $row['city'] . "</td></tr>";
}
mysql_close($con);
1 Comments
This constantly is amazing to me how site owners for example yourself can find some time along with the dedication to carry on producing terrific posts. Your blog issuperb and one of my personal must read personal blogs. I just wished to say thanks.
ReplyDeletePost a Comment