当前位置:Linux教程 - Php - 连动下拉菜单

连动下拉菜单

<html>
<head>
<title>List</title>
<meta http-equiv="Content-Type" content="text/html; charset=??????">
<?
if( !( $link = mysql_connect() ) ){
fatal("Could not connect to local MySQL server.");
}

if( !
mysql_select_db( "loaction", $link ) )
{
mysql_close( $link );
fatal("Could not select database <b>ParserSample</b>.");
}

$sql="select * from district order by locationid asc";
$query=mysql_query($sql,$link);
?>
<script language = "JavaScript">

var onecount;
onecount = 0;
subcat = new Array();
<?
$count
=0;
while(
$rs=mysql_fetch_array($query)){
?>
subcat[<?=$count;?>] = new Array("<?=$rs['districtname'];?>","<?=$rs['locationid'];?>","<?=$rs['districtid'];?>","<?=$rs['areacode']?>","<?=$rs['zipcode']?>");
<?
$count
++;
}
?>
onecount=<?=$count?>;

function GetCode(CityId){
var CityId=CityId;
var i;
for(i=0;i < onecount; i++){
if(subcat[i][2] == CityId){
document.FormData.AreaCode.value = subcat[i][3];
document.FormData.ZipCode.value = subcat[i][4];
}
}
}

function ClearCode(){
document.FormData.AreaCode.value = '';
document.FormData.ZipCode.value = '';
}

function getCity(locationid)
{
document.FormData.City.length = 0;

var locationid=locationid;

var i;
document.FormData.City.options[0] = new Option('==所选城市的地区==','');
for (i=0;i < onecount; i++)
{
if (subcat[i][1] == locationid)
{
document.FormData.City.options[document.FormData.City.length] = new Option(subcat[i][0], subcat[i][2]);
}
}

}
</script>
</head>
<body>
<form name="FormData" method="post" action="">
分类:<select name="Province" onChange="getCity(document.FormData.Province.options[document.FormData.Province.selectedIndex].value);ClearCode();" size="1">
<option selected>请选择你所在的省份</option>
<?
$sql1
= "select * from loaction";
$query=mysql_query($sql1,$link);
$a=mysql_num_rows($query);
while(
$rs1=mysql_fetch_array($query)){
?>
<option value="<?=$rs1['loactionid'];?>"><?=$rs1['loactionname'];?></option>
<?}?>
</select>
<select name="City" onChange="GetCode(document.FormData.City.options[document.FormData.City.selectedIndex].value);">
<option selected value="">==所有地区==</option>
</select>
<INPUT TYPE="text" NAME="AreaCode"><INPUT TYPE="text" NAME="ZipCode">
<INPUT TYPE="submit">
</form>
</body>
</html>
<?
if(!empty($_POST['City'])){
echo
$_POST['City'];
}
?>


表结构:

#
# 表的结构 `district`
#
# 创建时间: 2003 年 08 月 02 日 11:21
# 最后更新时间: 2003 年 08 月 02 日 11:21
#

CREATE TABLE `district` (
`locationid` int(10) NOT NULL default '0',
`districtid` int(10) NOT NULL auto_increment,
`areacode` varchar(4) NOT NULL default '',
`zipcode` varchar(6) NOT NULL default '',
`districtname` varchar(50) NOT NULL default '',
KEY `districtid` (`districtid`)
) TYPE=MyISAM AUTO_INCREMENT=7 ;

#
# 表`district`中数据locationid——省id;districtid——市id;districtname——市名称; areacode ——市区号;zipcode——市邮政编码
#

# --------------------------------------------------------

#
# 表的结构 `loaction`
#
# 创建时间: 2003 年 08 月 02 日 09:28
# 最后更新时间: 2003 年 08 月 02 日 10:36
#

CREATE TABLE `loaction` (
`loactionid` int(10) NOT NULL auto_increment,
`loactionname` varchar(50) NOT NULL default '',
KEY `loactionid` (`loactionid`)
) TYPE=MyISAM AUTO_INCREMENT=4 ;

#
# 表`loaction`中数据,loactionid——省id;loactionname——省名称
#

#表