ajax php實現(xiàn)三級聯(lián)動的方法:首先創(chuàng)建一個test數(shù)據(jù)庫并創(chuàng)建三張表;然后初始化所有的省份;接著把當前的省份id通過ajax發(fā)出請求傳遞到服務端的程序中;最后查詢數(shù)據(jù)庫并進行必要的處理顯示即可。
推薦:《php視頻教程》
案例涉及到數(shù)據(jù)庫,數(shù)據(jù)庫設計如下:
首先創(chuàng)建一個test數(shù)據(jù)庫,內容如下:
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);對數(shù)據(jù)庫說明:我創(chuàng)建了三張表,分別是?。╬rovince),市(city),縣(county),插入了幾條測試數(shù)據(jù),當然你也可以設計一張表,效率當然沒一張表好,所以不建議使用,看你個人習慣。
實現(xiàn)過程并不是很難,思路如下:
1) 初始化所有的省份,這個可以直接從數(shù)據(jù)庫中查詢出來省份
2)當用戶選擇省份的時候觸發(fā)事件,把當前的省份的id通過ajax發(fā)出請求傳遞到服務端的程序中
3)服務端根據(jù)客戶端的請求,查詢數(shù)據(jù)庫,并按照一定的格式返回給客戶端
4)客戶端獲取服務端的數(shù)據(jù),進行必要的處理顯示出來
創(chuàng)建select.php (代碼簡陋,只是實現(xiàn)功能而已,說明原理即可!)
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>三級聯(lián)動(作者: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