得到了解决方案,代码如下:
<?php
/* Written for Khaoskreations 2010-06-13 */
//db connect code
require "config.php"; // Your Database details
$query = "SELECT * FROM Carlist ";
$result = mysql_query($query);
//==============================================================================build arrays from db
$Make = array();
$Model = array();
$Version = array();
if (!$result) echo "Error: ".mysql_error();
else {
$Make_group = "";
$Model_group = "";
$num = mysql_num_rows($result);
for ($i = 0;$i<$num;$i++) {
$r = mysql_fetch_array($result);
if ($r['Make'] != $Make_group) {
$Make[$r['Make']] = $r['Make'];
$Model[$r['Make']][$r['Model']] = $r['Model'];
$Version[$r['Make']][$r['Model']][$r['Version']] = $r['Version'];
$Make_group = $r['Make'];
$Model_group = $r['Make'];
} else if ($r['Model'] != $Model_group) {
$Model[$r['Make']][$r['Model']] = $r['Model'];
$Version[$r['Make']][$r['Model']][$r['Version']] = $r['Version'];
$Model_group = $r['Make'];
} else {
$Version[$r['Make']][$r['Model']][$r['Version']] = $r['Version'];
}
}
}
//==============================================================================create scripts
$script = "\n<script type='text/javascript'>\n";
//build last name array
$script .= "var Make = new Array(";
$list = "";
foreach ($Make as $key => $val) {
$list .= " \"".$val."\",";
//echo $val ."<br />";
}
$list = substr($list, 0, -1);
$list .= " );\n";
$script .= $list;
$list = "";
//build Model array
$script .= "\nvar Model = new Array();";
foreach ($Model as $Maken => $fary) {
$list .= "\nModel[\"".$Maken."\"] = new Array(";
foreach ($fary as $key => $value) {
$list .= " \"". $value . "\",";
}
$list = substr($list, 0, -1);
$list .= " );";
}
$script .= $list;
$list = "";
//build Version array
$list = "\nvar Version = new Array();";
foreach ($Version as $Maken => $fary) {
$list .= "\nVersion[\"".$Maken."\"] = new Array();";
foreach ($fary as $Modeln => $aary) {
$list .= "\nVersion[\"".$Maken."\"][\"".$Modeln."\"] = new Array(";
foreach ($aary as $key => $value) {
$list .= " \"" . $value . "\",";
}
$list = substr($list, 0, -1);
$list .= " );";
}
}
$script .= $list;
//script functions to update tiers
$script .= <<< EOSCRIPT
function resetForm(theForm) {
theForm.Makes.options[0] = new Option("--- Select Make ---", "");
for (var i=0; i<Make.length; i++) {
theForm.Makes.options[i+1] = new Option(Make[i], Make[i]);
}
theForm.Makes.options[0].selected = true;
theForm.Models.options[0] = new Option("--- Select Model ---", "");
theForm.Models.options[0].selected = true;
theForm.Version.options[0] = new Option("--- Select Version ---", "");
theForm.Version.options[0].selected = true;
document.getElementById("selectbox").innerHTML = "";
}
function reloadForm(theForm,Manufacturer,product,specific) {
theForm.Makes.options[0] = new Option("--- Select Version ---", "");
for (var i=0; i<Make.length; i++) {
theForm.Makes.options[i+1] = new Option(Make[i], Make[i]);
if (Manufacturer == Make[i]) theForm.Makes.options[i+1].selected = true;
}
document.getElementById("selectbox").innerHTML = "here";
var fn = Model[Manufacturer];
theForm.Models.options.length = 0;
for (var i=0; i<fn.length; i++) {
theForm.Models.options[i] = new Option(fn[i], fn[i]);
if (product == fn[i]) {
theForm.Models.options[i].selected = true;
}
}
var Versions = Version[Manufacturer][product];
theForm.Version.options.length = 0;
for (var i=0; i<Versions.length; i++) {
theForm.Version.options[i] = new Option(Versions[i], Versions[i]);
if (specific == Versions[i][0]) {
theForm.Version.options[i].selected = true;
}
}
document.getElementById("selectbox").innerHTML = "You selected person #"+specific;
}
function updateproducts(theForm) {
var lname = theForm.Makes.options[theForm.Makes.options.selectedIndex].value;
var fnames = Model[lname];
theForm.Models.options.length = 0;
for (var i=0; i<fnames.length; i++) {
theForm.Models.options[i] = new Option(fnames[i], fnames[i]);
}
var findex = theForm.Models.options[theForm.Models.options.selectedIndex].value;
var Versions = Version[lname][findex];
theForm.Version.options.length = 0;
for (var i=0; i<Versions.length; i++) {
theForm.Version.options[i] = new Option(Versions[i], Versions[i]);
}
theForm.Version.options[0].selected = true;
}
function updateVersion(theForm) {
var lname = theForm.Makes.options[theForm.Makes.options.selectedIndex].value;
var findex = theForm.Models.options[theForm.Models.options.selectedIndex].value;
var Versions = Version[lname][findex];
theForm.Version.options.length = 0;
for (var i=0; i<Versions.length; i++) {
theForm.Version.options[i] = new Option(Versions[i], Versions[i]);
}
theForm.Version.options[0].selected = true;
}
</script>
EOSCRIPT;
$reload_script = "";
if ($_POST['Makes'] != "" && $_POST['Models'] != "") {
$reload_script .= "<script type='text/javascript'> "
."reloadForm(document.vehicleform,'".$_POST['Makes']."','".$_POST['Models']."',".$_POST['Version'].");"
."</script>";
} else {
$reload_script .= "<script type='text/javascript'>"
."resetForm(document.vehicleform);"
."</script>";
}
//============================================================================== build page
$page = <<< EOPAGE
<html>
<head>
{$script}
</head>
<body>
<form method="post" action="test3.php" name='vehicleform'>
<select name='Makes' onchange="updateproducts(this.form)"></select><BR>
<select name="Models" onchange="updateVersion(this.form)"></select><BR>
<select name="Version"></select>
<input type='button' value='Reset' onclick='resetForm(document.vehicleform)'/>
<input type='submit' value='Reload'/>
</form>
<div id='selectbox'></div>
{$reload_script}
</body>
</html>
EOPAGE;
echo $page;
?>