我正在研究制造型号年份汽车数据库,并且我有以下表格,可以根据制造选择型号。
<?
$hostname = "";
$database = "";
$username = "";
$password = "";
$mysql_link = mysql_connect($hostname,$username,$password) or die ("can't connect to mysql");
$mysql_select = mysql_select_db($database,$mysql_link) or die ("can't select db");
?>
<html>
<head>
<title>Dynamic Dropdown</title>
<script language="javascript">
function setOptions(chosen) {
var selbox = document.myform.selectmodel;
selbox.options.length = 0;
if (chosen == "0") {
selbox.options[selbox.options.length] = new Option('First select a car','0');
}
<?
$car_result = mysql_query("SELECT * FROM make") or die(mysql_error());
while(@($c=mysql_fetch_array($car_result)))
{
?>
if (chosen == "<?=$c['make'];?>") {
<?
$c_id = $c['make'];
$mod_result = mysql_query("SELECT * FROM model WHERE make='$c_id'") or die(mysql_error());
while(@($m=mysql_fetch_array($mod_result)))
{
?>
selbox.options[selbox.options.length] = new
Option('<?=$m['model'];?>','<?=$m['model'];?>');
<?
}
?>
}
<?
}
?>
}
</script>
</head>
<body>
<form name="myform"><div align="center">
<p>
<select name="selectcar" size="1"
onchange="setOptions(document.myform.selectcar.options
[document.myform.selectcar.selectedIndex].value);">
<option value="0" selected>Select a car</option>
<?
$result = mysql_query("SELECT * FROM make") or die(mysql_error());
while(@($r=mysql_fetch_array($result)))
{
?>
<option value="<?=$r['make'];?>">
<?=$r['make'];?>
</option>
<?
}
?>
</select>
<br>
<br>
<select name="selectmodel" size="1">
<option value=" " selected>First select a car</option>
</select>
<br>
<br>
<select name="optthree" size="1">
<option value=" " selected="selected">Please select one of the options above first</option>
</select>
</p>
<p>
<input type="button" name="go" value="Value Selected"
onclick="alert(document.myform.selectmodel.options
[document.myform.selectmodel.selectedIndex].value);">
</p>
</div></form>
</body>
</html>
我需要添加 javascript 代码和表单代码,以使第三个下拉框根据品牌和型号显示年份。
感谢你的协助。
这是用于填充和选择年份下拉列表的更新后的 javascript
<script language="javascript">
function setOptions(chosen) {
var selbox = document.myform.selectmodel;
selbox.options.length = 0;
if (chosen == "0") {
selbox.options[selbox.options.length] = new Option('First select a car','0');
}
<?
$car_result = mysql_query("SELECT * FROM make") or die(mysql_error());
while(@($c=mysql_fetch_array($car_result)))
{
?>
if (chosen == "<?=$c['make'];?>") {
<?
$c_id = $c['make'];
$mod_result = mysql_query("SELECT * FROM model WHERE make='$c_id'") or die (mysql_error());
while(@($m=mysql_fetch_array($mod_result)))
{
?>
selbox.options[selbox.options.length] = new
Option('<?=$m['model'];?>','<?=$m['model'];?>');
<?
}
?>
if (chosen == "<?=$m['make'];?>") {
<?
$m_id = $c['make'];
$mod_result = mysql_query("SELECT * FROM year WHERE make='$c_id' AND model='$m_id'") or die(mysql_error());
while(@($y=mysql_fetch_array($year_result)))
{
?>
selbox.options[selbox.options.length] = new
Option('<?=$y['year'];?>','<?=$y['year'];?>');
<?
}
<?
}
?>
}
</script>