ajax php如何实现三级联动

发布时间:2025-08-26 点击:24
ajax php实现三级联动的方法:首先创建一个test数据库并创建三张表;然后初始化所有的省份;接着把当前的省份id通过ajax发出请求传递到服务端的程序中;最后查询数据库并进行必要的处理显示即可。
推荐:《php视频教程》
案例涉及到数据库,数据库设计如下:
首先创建一个test数据库,内容如下:
create table if not exists `province` ( `province_id` int(2) not null auto_increment, `province_name` varchar(20) not null, primary key (`province_id`)) engine=innodb default charset=utf8 auto_increment=3 ; insert into `province` (`province_id`, `province_name`) values(1, '安徽'),(2, '浙江'); create table if not exists `city` ( `city_id` int(4) not null auto_increment, `city_name` varchar(20) not null, `province_id` int(4) not null, primary key (`city_id`)) engine=innodb default charset=utf8 auto_increment=5 ; insert into `city` (`city_id`, `city_name`, `province_id`) values(1, '合肥', 1),(2, '安庆', 1),(3, '南京', 2),(4, '徐州', 2); create table if not exists `county` ( `county_id` int(4) not null auto_increment, `county_name` varchar(20) not null, `city_id` int(4) not null, primary key (`county_id`)) engine=innodb default charset=utf8 auto_increment=5 ; insert into `county` (`county_id`, `county_name`, `city_id`) values(1, '怀宁', 2),(2, '望江', 2),(3, '肥东', 1),(4, '肥西', 1);对数据库说明:我创建了三张表,分别是省(province),市(city),县(county),插入了几条测试数据,当然你也可以设计一张表,效率当然没一张表好,所以不建议使用,看你个人习惯。
实现过程并不是很难,思路如下:
1) 初始化所有的省份,这个可以直接从数据库中查询出来省份
2)当用户选择省份的时候触发事件,把当前的省份的id通过ajax发出请求传递到服务端的程序中
3)服务端根据客户端的请求,查询数据库,并按照一定的格式返回给客户端
4)客户端获取服务端的数据,进行必要的处理显示出来
创建select.php (代码简陋,只是实现功能而已,说明原理即可!)
1 <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">2 <html>3 <head>4 <title>三级联动(作者:mckee - www.phpddt.com)</title>5 <meta http-equiv="content-type"content="text/html; charset=utf-8"/>6 <script>7 function createajax(){8 var xmlhttp = false;9 if (window.xmlhttprequest){10 xmlhttp = new xmlhttprequest();11 }else if(window.activexobject){12 try{13 xmlhttp = new activexobject("msxml2.xmlhttp");14 }catch(e){15 try{16 xmlhttp = new activexobject("microsoft.xmlhttp");17 }catch(e){18 xmlhttp = false;19 }20 }21 }22 return xmlhttp; 23 }24 25 var ajax = null;26 function getcity(province_id){27 ajax = createajax();28 ajax.onreadystatechange=function(){29 if(ajax.readystate == 4){30 if(ajax.status == 200){ 31 var cities = ajax.responsexml.getelementsbytagname("city");32 $('city').length = 0;33 var myoption = document.createelement("option");34 myoption.value = "";35 myoption.innertext = "--请选择城市--";36 $('city').appendchild(myoption);37 for(var i=0;i<cities.length;i ){38 var city_id = cities[i].childnodes[0].childnodes[0].nodevalue;39 var city_name = cities[i].childnodes[1].childnodes[0].nodevalue;40 var myoption = document.createelement("option");41 myoption.value = city_id;42 myoption.innertext = city_name;43 $('city').appendchild(myoption);44 }45 }46 }47 }48 49 ajax.open("post","selectpro.php",true);50 ajax.setrequestheader("content-type", "application/x-www-form-urlencoded");51 ajax.send("province_id=" province_id);52 53 }54 55 function getcounty(city_id){56 ajax = createajax();57 ajax.onreadystatechange=function(){58 if(ajax.readystate == 4){59 if(ajax.status == 200){60 61 var cities = ajax.responsexml.getelementsbytagname("county");62 $('county').length = 0;63 var myoption = document.createelement("option");64 myoption.value = "";65 myoption.innertext = "--请选择县--";66 $('county').appendchild(myoption);67 for(var i=0;i<cities.length;i ){68 var city_id = cities[i].childnodes[0].childnodes[0].nodevalue;69 var city_name = cities[i].childnodes[1].childnodes[0].nodevalue;70 var myoption = document.createelement("option");71 my

云服务器怎么进去ip
企业云服务器在哪买
云服务器公共镜像的选择
谷歌 SEO 入门指南 2018 年版(五):站点移动化及推广
重启动服务器后无法远程连接上
原账户所有人科海集团当时并为未注册公司
网页建设在速度方向不能落后
域名长短有什么影响