jQuery+php+mysql,轻松实现ajax无刷新省市二级联动

用jQuery的ajax方法,结合PHP,轻松实现省市级的二级联动:

新建area.php

<?
 $dbc=mysqli_connect("localhost","xxxx","xxxxxxxxxx","test") or die("can not connect to mysql ".mysqli_error());
 mysqli_query($dbc,"set names utf8");
 header("Content-Type:text/html;charset=utf-8");
 
 if($_REQUEST['do']=="search" && $_REQUEST['pid']){
  $str="";
  $str='<select id="down_area" name="down_area">';
 
 $r="select areaid,name from `china_areas` where parentid='".$_REQUEST['pid']."' order by vieworder asc;";
 $q=mysqli_query($dbc,$r);
  while(list($areaid,$name)=mysqli_fetch_array($q,MYSQLI_NUM)){
  
  $str.='<option value="'.$areaid.'">'.$name.'</option>';
  
  }
  
  $str.='</select>';
  
  echo $str;exit;
  }
 ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>无刷新二级联动</title>
 <script src="js/jquery-1.6.2.min.js" type="text/javascript"></script>
 <script language="javascript">
 function down_area(obj){ 
 $.ajax({
    type: "POST",
    url: "area.php",
    data: "do=search&pid="+obj.value,
    success: function(msg){
   $("#area_list").html(msg);
    }
 });
  }
 </script>
 <style>
 body{font:12px Arial, "微软雅黑", sans-serif;color:#666;line-height:22px;}
 </style>
 </head>
 
 <body>
 <p style="height:250px;"> </p>
 <div align="center">
 地区:
 <select id="area" name="area" οnchange="down_area(this);">
 <?
 $r="select areaid,name from `china_areas` where parentid='0' order by vieworder asc;";
 $q=mysqli_query($dbc,$r);
 while(list($areaid,$name)=mysqli_fetch_array($q,MYSQLI_NUM)){
 ?>
 <option value="<?=$areaid;?>"><?=$name;?></option>
 <?
 }
 ?>
 </select>   <span id="area_list"></span>
 </div>
 </body>

效果如图:

下拉选择广东省后:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值