Here is the code. Look at my previous reply for its functions.
1. download the Ajax class, name simple ajax.js and store that in an ajax subdirectory.
2. create the test table cars with the following sql
-
create table cars (id int(2) primary key auto_increment,
-
year int(2),
-
make varchar(20),
-
description varchar(255));
-
insert into cars (year, make, description) values(1961, 'honda', 'Description Honda 1961');
-
insert into cars (year, make, description) values(1962, 'honda', 'Description Honda 1962');
-
insert into cars (year, make, description) values(1963, 'honda', 'Description Honda 1963');
-
insert into cars (year, make, description) values(1963, 'ford', 'Description Ford 1963');
-
insert into cars (year, make, description) values(1964, 'ford', 'Description Ford 1964');
-
insert into cars (year, make, description) values(1967, 'ford', 'Description Ford 1967');
-
insert into cars (year, make, description) values(1961, 'mazda', 'Description Mazda 1961');
-
insert into cars (year, make, description) values(1962, 'mazda', 'Description Mazda 1962');
-
Then run CARS.PHP to invoke the functions.
CARS.PHP
[php]
<html><head><title>Test drop down cars</title>
</style>
<script type="text/javascript" src="ajax/ajax.js"></script>
<script type="text/javascript">
var ajax = new sack();
var selYear;
function getMake(sel) {
selYear = sel.options[sel.selectedIndex].value;
if(selYear.length>0){
ajax.requestFile = 'getCarInfo.php?getmake=1&year='+selYear; // Specifying what to get
ajax.onCompletion = createDropdownMake; // Specify function that will be executed after file has been found
ajax.runAJAX(); // Execute AJAX function
}
}
function createDropdownMake() {
document.getElementById("divmsg").innerHTML = ajax.response;
}
function getDesc(sel) {
var selMake = sel.options[sel.selectedIndex].value;
if(selMake.length>0){
ajax.requestFile = 'getCarInfo.php?getdesc=1&make='+selMake+'&year='+ selYear; // Specifying what to get
ajax.onCompletion = createDesc; // Specify function that will be executed after file has been found
ajax.runAJAX(); // Execute AJAX function
}
}
function createDesc() {
document.getElementById("divmsg").innerHTML = ajax.response;
}
</script>
</head>
<body>
<?php
// ================================================== ===
// Populate the YEARS selection list from the database
// ================================================== ===
echo '<u><b>View the description of a car here</b></u>
<form action="" method="post">
Select year:
<select id="years" name="years" onchange="getMake(this)">
<option value="">Select</option>';
$conn = mysql_connect("localhost","xxxx","yyyyy")
or die("Could not connect to the database server: ".mysql_error());
mysql_select_db("zzz",$conn)
or die("Could not select the database: " . mysql_error());
$res = mysql_query("SELECT year FROM cars GROUP BY year ORDER BY year")
or die("Invalid query: " . mysql_query());
while ($row = mysql_fetch_assoc($res)) {
$yr = $row['year'];
echo "<option value=$yr>$yr</option>";
}
echo '</select>';
?>
<div id="divmsg" />
</form>
</body></html>
[/php]
GETCARINFO.PHP is called by CARS.PHP
[php]
<?php
$conn = mysql_connect("localhost","xxxx","yyyy")
or die("Could not connect to the database server: ".mysql_error());
mysql_select_db("zzz",$conn)
or die("Could not select the database: " . mysql_error());
// Get the make info
if(isset($_GET['getmake']) AND isset($_GET['year'])){
$yr = $_GET['year'];
$res = mysql_query("SELECT make FROM cars WHERE year=$yr GROUP BY make ORDER BY make")
or die("Invalid query: " . mysql_query());
echo 'Select make: ';
echo '<select id="make" name="make" onchange="getDesc(this)">';
echo '<option value="">Select</option>';
while ($row = mysql_fetch_assoc($res)) {
$mk = $row['make'];
echo "<option value=$mk>$mk</option>";
}
echo '</select>';
}
// Get the description
if(isset($_GET['getdesc']) AND isset($_GET['make']) AND isset($_GET['year'])){
$mk = $_GET['make'];
$yr = $_GET['year'];
$res = mysql_query("SELECT description FROM cars WHERE year=$yr AND make='$mk'")
or die("Invalid query: " . mysql_query());
echo "Make: &n bsp; $mk<br />";
while ($row = mysql_fetch_assoc($res)) {
$ds = $row['description'];
echo "Description: $ds";
}
}
?>
[/php]
All is tested and it works. Any questions left: in this thread, so everyone can follow it. Good luck.
Ronald :cool: