加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

实现三级联动菜单(ajax)

发布时间:2020-12-15 21:01:30 所属栏目:百科 来源:网络整理
导读:如何实现三级联动菜单? 首先先建一个全国各省市县数据库(http://download.csdn.net/detail/feilong_12/5141762)这里有现成的数据库直接考过来就可以用而且 比较全面的哦。 下面是:三级联动的html文件(里面写ajax) html head script function getArea(v

如何实现三级联动菜单?

首先先建一个全国各省市县数据库(http://download.csdn.net/detail/feilong_12/5141762)这里有现成的数据库直接考过来就可以用而且比较全面的哦。

下面是:三级联动的html文件(里面写ajax)

<html>
<head>
<script>
function getArea(val,table){

var xhr;
if(window.ActiveXObject){
xhr=new ActiveXOject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}

var url="ajax.php";
xhr.open("post",url,true);
xhr.onreadystatechange=callback;
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send("val="+val+"&table="+table);
function callback(){
if(xhr.readyState==4){
if(xhr.status==200){
//alert(xhr.responseText);
document.getElementById(table).innerHTML=xhr.responseText;
}
}
}
}
</script>
</head>
<body onload="getArea('','t_province')">
<select id="t_province" onchange="getArea(this.value,'t_city')"></select>省
<select id="t_city" onchange="getArea(this.value,'t_district')"></select>市
<select id="t_district"></select>县
</body>
</html>


ajax.php文件


<?php
mysql_connect('localhost','root','123');
mysql_select_db('china');
mysql_query("set names utf8");
$val=$_POST['val'];
$table=$_POST['table'];
if($table=='t_province'){
$sql="select ProName from $table order by ProSort";
$result=mysql_query($sql);
$rows=array();
while($row=mysql_fetch_row($result)){
$rows[]=$row;

echo "<option>$row[0]</option>";
}
}else if($table=='t_city'){
$sql="select CityName from t_city where ProId=(select ProID from t_province where ProName='$val')";
$result=mysql_query($sql);
$rows=array();
while($row=mysql_fetch_row($result)){
$rows[]=$row;
echo "<option>$row[0]</option>";
}var_dump($rows);
}
else if($table=='t_district'){
$sql="select DisName from t_district where CityId=(select CityID from t_city where CityName='$val')";
$result=mysql_query($sql);
$rows=array();
while($row=mysql_fetch_row($result)){
$rows[]=$row;
echo "<option>$row[0]</option>";
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读