关闭

ajax-jq-php(mysql)四级省市级联

标签: ajax-mysql
52人阅读 评论(0) 收藏 举报
分类:
function getprov(){
var prov=$("#pro");
var str="";
ajax("tcentre.php",str,  
        function(data){  
          var obj = eval('('+data+')');
            $("#pro").empty();  
            $("#pro").prepend("<option value='-1'>请选择市</option>");           
            for(var p in obj){  
                $("#pro").append("<option value="+obj[p].zonecode+">"+obj[p].zonename+"</option>");  

            }  
        }  
    );
}

function getcity(){  
    var a = $("#pro").val();
    var str="code="+a;  

    ajax("tcentre1.php",str,  
        function(data){  
            var obj = eval('('+data+')');
            $("#city").empty();  
            $("#city").prepend("<option value='-1'>请选择市</option>");           
            for(var p in obj){  
                $("#city").append("<option value="+obj[p].zonecode+">"+obj[p].zonename+"</option>");  

            }  
        }  
    );    
}  
function getq(){  
    var a = $("#city").val();  
    var str="code="+a;

    ajax("tcentre2.php",str,
        function(data){  
           var obj = eval('('+data+')');  
            $("#zone").empty();  
            $("#zone").prepend("<option value='-1'>请选择区</option>");           
            for(var p in obj){  
                $("#zone").append("<option value="+obj[p].zonecode+">"+obj[p].zonename+"</option>");  

            }  
        }  
    );  
}  
function gets(){  
    var a = $("#zone").val();  
    var str="code="+a;

    ajax("tcentre3.php",str,
        function(data){  
           var obj = eval('('+data+')');  
            $("#street").empty();  
            $("#street").prepend("<option value='-1'>请选择街道</option>");           
            for(var p in obj){  
                $("#street").append("<option value="+obj[p].zonecode+">"+obj[p].zonename+"</option>");  

            }  
        }  
    );  
}  


function ajax(url,poststr,success) {

  var xmlhttp =  new XMLHttpRequest();
  xmlhttp.open("POST",url);

  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");


  xmlhttp.send(poststr);

  xmlhttp.onreadystatechange = function () {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
      success(xmlhttp.responseText);

    }
  }
}

body onload=”getprov()”
select name=”province” id=”pro” onchange=”getcity()”
option 请选择省 option
select
select name=”city” id=”city” onchange=”getq()”
option 请选择市 option
select
select name=”zone” id=”zone” onchange=”gets()”
option请选择区/option
select
select name=”street” id=”street”
option 请选择街道 option
select
body

 include 'db.php';
 $db = new DB();
  $sql = "select * from diqudaima where areaid=1";
 $res=$db->get($sql);
 $arr = array();
 for($i=1;$i<count($res);$i++){
  $arr[$i-1]['province'] = $res[$i-1][1];
  $arr[$i-1]['city'] = $res[$i-1][2];
  $arr[$i-1]['zonename'] = $res[$i-1][5];
  $arr[$i-1]['zonecode'] = $res[$i-1][4];
 }
 $a = json_encode($arr);
 echo $a;


include 'db.php';
      $db = new DB();
      $code = $_POST['code'];
      $sql = "select * from diqudaima where areaid='2' and zonecode like '$code%'";
       $res=$db->get($sql);
      $arr =array();
      for($i=1;$i<count($res);$i++){
      $arr[$i-1]['province'] = $res[$i-1][1];
      $arr[$i-1]['city'] = $res[$i-1][2];
      $arr[$i-1]['zonename'] = $res[$i-1][5];
      $arr[$i-1]['zonecode'] = $res[$i-1][4];
           }
           $a = json_encode($arr);
           echo $a;


include 'db.php';
      $db = new DB();
      $code = $_POST['code'];
      $sql = "select * from diqudaima where areaid='3' and zonecode like '$code%'";
       $res=$db->get($sql);
      $arr =array();
           for($i=1;$i<count($res);$i++){
      $arr[$i-1]['province'] = $res[$i-1][1];
      $arr[$i-1]['city'] = $res[$i-1][2];
      $arr[$i-1]['zonename'] = $res[$i-1][5];
      $arr[$i-1]['zonecode'] = $res[$i-1][4];
           }
           $a = json_encode($arr);
           echo $a;


include 'db.php';
      $db = new DB();
      $code = $_POST['code'];
      $sql = "select * from diqudaima where areaid='4' and zonecode like '$code%'";
       $res=$db->get($sql);
      $arr =array();
           for($i=1;$i<count($res);$i++){
      $arr[$i-1]['province'] = $res[$i-1][1];
      $arr[$i-1]['city'] = $res[$i-1][2];
      $arr[$i-1]['zonename'] = $res[$i-1][5];
      $arr[$i-1]['zonecode'] = $res[$i-1][4];
           }
           $a = json_encode($arr);
           echo $a;
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:200次
    • 积分:33
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档