Dear Friends,
Here I have exemplified a shop with car brands and models. When you select Brand from one combo only those models matching the brands will be displayed in the other combo box.
SQL Database and Tables required
--
-- Database: `carcare`
--
CREATE TABLE IF NOT EXISTS `brand` (
`bid` varchar(5) NOT NULL,
`bname` varchar(20) NOT NULL,
PRIMARY KEY (`bid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `model` (
`bid` varchar(5) NOT NULL,
`mid` varchar(5) NOT NULL,
`mname` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Make sure you have data in brand and model tables
1. database.php
<?php
$cn=mysql_connect("localhost","root","") or die("Could not Connect My Sql");
mysql_select_db("carcare",$cn) or die("Could connect to Database");
?>
2. servicereq.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Car Service</title>
<script>
function loadDoc() {
//alert('HUP');
var key=document.frmsearch.brand.value;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("a1").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "selectmodel.php?key="+key, true);
xhttp.send();
}
</script>
</head>
<body>
<table>
<form name=frmsearch method=post>
<tr><td>Select Brand</td><td><select name="brand" onChange="loadDoc()"><option>select</option>
<?php
include("database.php");
$qr="select * from brand";
$data=mysql_query($qr,$cn);
while($row=mysql_fetch_array($data))
{
echo"<option value='$row[bid]'>".$row[bname]."</option>";
}
?>
</select>
</td>
</tr>
<tr><td>Select Model</td><td><div id=a1></div></td></tr>
</table>
</body>
</html>
3. selectmodel.php
<select name="model">
<?php
$bid=$_GET['key'];
include("database.php");
$qr="select * from model where bid like '%$bid%'";
$data=mysql_query($qr,$cn);
while($row=mysql_fetch_array($data))
{
echo"<option>".$row[mname]."</option>";
}
?>
</select>
Here I have exemplified a shop with car brands and models. When you select Brand from one combo only those models matching the brands will be displayed in the other combo box.
SQL Database and Tables required
--
-- Database: `carcare`
--
CREATE TABLE IF NOT EXISTS `brand` (
`bid` varchar(5) NOT NULL,
`bname` varchar(20) NOT NULL,
PRIMARY KEY (`bid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `model` (
`bid` varchar(5) NOT NULL,
`mid` varchar(5) NOT NULL,
`mname` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Make sure you have data in brand and model tables
1. database.php
<?php
$cn=mysql_connect("localhost","root","") or die("Could not Connect My Sql");
mysql_select_db("carcare",$cn) or die("Could connect to Database");
?>
2. servicereq.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Car Service</title>
<script>
function loadDoc() {
//alert('HUP');
var key=document.frmsearch.brand.value;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("a1").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "selectmodel.php?key="+key, true);
xhttp.send();
}
</script>
</head>
<body>
<table>
<form name=frmsearch method=post>
<tr><td>Select Brand</td><td><select name="brand" onChange="loadDoc()"><option>select</option>
<?php
include("database.php");
$qr="select * from brand";
$data=mysql_query($qr,$cn);
while($row=mysql_fetch_array($data))
{
echo"<option value='$row[bid]'>".$row[bname]."</option>";
}
?>
</select>
</td>
</tr>
<tr><td>Select Model</td><td><div id=a1></div></td></tr>
</table>
</body>
</html>
3. selectmodel.php
<select name="model">
<?php
$bid=$_GET['key'];
include("database.php");
$qr="select * from model where bid like '%$bid%'";
$data=mysql_query($qr,$cn);
while($row=mysql_fetch_array($data))
{
echo"<option>".$row[mname]."</option>";
}
?>
</select>