当前位置:首页 > 联系方式 >

PHP+Ajax+Mysql联合实现省市区三级联动

PHP+Ajax+Mysql联合实现省市区三级联动

1.MySQL数据文件导入,该文件会上传到我的资源库 dh_areas

2.HTML

<select id="province" name="province" onchange="selcity(this.value,this.id)"> <option value="">请选择</option> <volist name="province" id="vo" > <option value="{$vo.area_id}">{$vo.area_name}</option> </volist> </select> <select id="city" name="city" onchange="selcity(this.value,this.id)"> <option value="">请选择</option> </select> <select id="country" name="country"> <option value="">请选择</option> </select>

3.ajax

var selcityUrl = "__APP__/Home/RegisterInfo/getArea"; function selcity(areaId, obgId) { $.ajax({ type: "post", url: selcityUrl, async: false, data: { "areaId": areaId }, success: function(json) { var json = eval('(' + json + ')'); if (obgId == "province") { $("#city").empty(); var optionObg = "<option value=''>请选择</option>"; for (var i = 0; i < json.count; i++) { optionObg += "<option value='" + json[i].area_id + "'>" + json[i].area_name + "</option>"; } $("#city").append(optionObg); } else { $("#country").empty(); var optionObg = "<option value=''>请选择</option>"; for (var i = 0; i < json.count; i++) { optionObg += "<option value='" + json[i].area_id + "'>" + json[i].area_name + "</option>"; } $("#country").append(optionObg); } } }); }

4.PHP

(1)获取省列表

public function register(){ //获取省级地区 $province=M('areas')->where(array('parent_id'=>1))->select(); $this->assign('province',$province); $this->display(); }

(2)获取市/县

public function getArea(){ $where['parent_id']=I('areaId'); $area=D('areas')->where($where)->select(); $area['count']=count($area); $this->ajaxReturn($area,'json'); }

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:湖北SEO http://hubei.4567w.com

上一篇:Python 100例

下一篇:最后一页