如何用ajax实现三级联动

liandong.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)){
      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)){
   echo "<option>$row[0]</option>";
  }
 }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)){
   echo "<option>$row[0]</option>";
  }
 }

 

liandong.html

 

<html>
<head>
<script>
function san(val,table){
    var xhr;
 if(window.ActiveXObject){
    xhr=new ActiveXObject("Microsoft.XMLHTTP");
 }else if(window.XMLHttpRequest){
   xhr=new XMLHttpRequest();
 }
 var url="liandong.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){
      document.getElementById(table).innerHTML=xhr.responseText;
    }
    }
 }
}
</script>
</head>
<body οnlοad="san('','t_province')">
<select id="t_province" οnchange="san(this.value,'t_city')">
</select>省
<select id="t_city" οnchange="san(this.value,'t_district')">
</select>市
<select id="t_district">
</select>县
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值