PHP+Mysql+Ajax+JS實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
基本思想就是:在JS動(dòng)態(tài)創(chuàng)建select控件的option,通過(guò)Ajax獲取在PHP從SQL數(shù)據(jù)庫(kù)獲取的省市區(qū)信息,代碼有點(diǎn)長(zhǎng),但很多都是類似的,例如JS中省、市、區(qū)獲取方法類似,PHP中通過(guò)參數(shù)不同執(zhí)行不同的select語(yǔ)句。
index.html代碼:
<!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>
<title>省市區(qū)三級(jí)聯(lián)動(dòng)</title>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<script src="scripts/thumbnails.js" type="text/javascript"></script>
</head>
<body>
<div id="description">
<select style="width:100px; " onchange="sech(this.id)" id="sheng">
<option value="province">請(qǐng)選擇省份</option>
</select>
<select onchange="sech(this.id)" id="shi">
<option value="city">請(qǐng)選擇市區(qū)</option>
</select>
<select id="xian">
<option value="county">請(qǐng)選擇縣鄉(xiāng)</option>
</select>
</div>
</div>
</body>
</html>
thumbnails.js代碼:
window.onload = getProvince;
function createRequest() {//Ajax于PHP交互需要對(duì)象
try {
request = new XMLHttpRequest();//創(chuàng)建一個(gè)新的請(qǐng)求對(duì)象;
} catch (tryMS) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}
return request;
}
function sech(id) {//省市改變時(shí)觸發(fā),select的onchange事件
var aa = document.getElementById(id);
if(id=="sheng"){
getCity(aa.value);//這里aa.value為省的id
}
if(id=="shi")
{
getCounty(aa.value);//這里aa.value為市的id
}
}
function getProvince() {//獲取所有省
request = createRequest();
if (request == null) {
alert("Unable to create request");
return;
}
var url= "getDetails.php?ID=0";//ID=0時(shí)傳遞至PHP時(shí)讓其獲取所有省
request.open("GET", url, true);
request.onreadystatechange = displayProvince; //設(shè)置回調(diào)函數(shù)
request.send(null); //發(fā)送請(qǐng)求
}
function getCity(id){//獲取省對(duì)應(yīng)的市
request = createRequest();
if (request == null) {
alert("Unable to create request");
return;
}
var url= "getDetails.php?ID=" + escape(id);
request.open("GET", url, true);
request.onreadystatechange = displayCity;
request.send(null);
}
function getCounty(id){//獲取市對(duì)應(yīng)的區(qū)
request = createRequest();
if (request == null) {
alert("Unable to create request");
return;
}
var url= "getDetails.php?ID=" + escape(id);
request.open("GET", url, true);
request.onreadystatechange = displayCounty;
request.send(null);
}
function displayProvince() {//將獲取的數(shù)據(jù)動(dòng)態(tài)增加至select
if (request.readyState == 4) {
if (request.status == 200) {
var a=new Array;
var b=request.responseText;//將PHP返回的數(shù)據(jù)賦值給b
a=b.split(",");//通過(guò)","將這一數(shù)據(jù)保存在數(shù)組a中
document.getElementById("sheng").length=1;
var obj=document.getElementById("sheng');
for(i=0;i
obj.options.add(new Option(a[i],i+1)); //動(dòng)態(tài)生成OPTION加到select中,第一個(gè)參數(shù)為T(mén)ext,第二個(gè)參數(shù)為Value值.
}
}
}
function displayCity() {//將獲取的數(shù)據(jù)動(dòng)態(tài)增加至select
if (request.readyState == 4) {
if (request.status == 200) {
var a=new Array;
var b=request.responseText;
a=b.split(",");
document.getElementById("shi").length=1;//重新選擇
document.getElementById("xian").length=1;//重新選擇
if(document.getElementById("sheng").value!="province"){
var obj=document.getElementById('shi');
for(i=0;i
obj.options.add(new Option(a[i], document.getElementById("sheng").value*100+i+1)); //ocument.getElementById("sheng").value*100+i+1對(duì)應(yīng)的是市的ID。
}
}
}
}
function displayCounty() {//將獲取的數(shù)據(jù)增加至select
if (request.readyState == 4) {
if (request.status == 200) {
var a=new Array;
var b=request.responseText;
a=b.split(",");
document.getElementById("xian").length=1;
if(document.getElementById("sheng").value!="province"&&document.getElementById("shi").value!="city"){
var obj=document.getElementById('xian');
for(i=0;i
obj.options.add(new Option(a[i],i+1001));
}
}
}
}
getDetails.php代碼:
<?php
header("Content-Type: text/html; charset=gb2312");
$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$connstr = "Provider=SQLOLEDB;Persist Security Info=False;User ID=root;Password=123456;Initial Catalog=area;Data Source=localhost";
if($_REQUEST['ID']==0){//獲得省列表
$conn->Open($connstr); //建立數(shù)據(jù)庫(kù)連接
$sqlstr = "select name from Province"; //設(shè)置查詢字符串
$rs = $conn->Execute($sqlstr); //執(zhí)行查詢獲得結(jié)果
$num_cols = $rs->Fields->Count(); //得到數(shù)據(jù)集列數(shù)
$Province=array();
$i=0;
while (!$rs->EOF) {
$Province[$i]=$rs->Fields['name']->Value.",";
$rs->MoveNext();
$i++;
}
foreach($Province as $val)
echo $val;
$conn->Close();
$rs = null;
$conn = null;
}
if($_REQUEST['ID']>0&&$_REQUEST['ID']<35){//獲得省對(duì)應(yīng)的市列表
$conn->Open($connstr); //建立數(shù)據(jù)庫(kù)連接
$sqlstr = "select name from City where cid=".$_REQUEST['ID']; //設(shè)置查詢字符串
$rs = $conn->Execute($sqlstr); //執(zhí)行查詢獲得結(jié)果
$num_cols = $rs->Fields->Count(); //得到數(shù)據(jù)集列數(shù)
$City=array();
$i=0;
while (!$rs->EOF) {
$City[$i]=$rs->Fields['name']->Value.",";
$rs->MoveNext();
$i++;
}
foreach($City as $val)
echo $val;
$conn->Close();
$rs = null;
$conn = null;
}
if($_REQUEST['ID']>100){//獲得省市對(duì)應(yīng)的縣列表
$conn->Open($connstr); //建立數(shù)據(jù)庫(kù)連接
$sqlstr = "select name from County where cid=".$_REQUEST['ID']; //設(shè)置查詢字符串
$rs = $conn->Execute($sqlstr); //執(zhí)行查詢獲得結(jié)果
$num_cols = $rs->Fields->Count(); //得到數(shù)據(jù)集列數(shù)
$County=array();
$i=0;
while (!$rs->EOF) {
$County[$i]=$rs->Fields['name']->Value.",";
$rs->MoveNext();
$i++;
}
foreach($County as $val)
echo $val;
$conn->Close();
$rs = null;
$conn = null;
}
?>
數(shù)據(jù)庫(kù)設(shè)計(jì),表格Province表,City表,County表。
要求:Province表需要id和name,id建議從1至34,例如北京id為1,廣東id為2,以此類推;
City表需要id,name和cid,id為cid*100+1,cid為該市的上級(jí),例如深圳的上級(jí)為廣東省,cid為2的話,深圳的id就是201,以此類推。
County表需要id,name和cid,因?yàn)槭侨?jí)的關(guān)系,id可以隨意,建議從10001開(kāi)始自增。cid為所在上級(jí),例如寶安區(qū)的cid為201,龍崗區(qū)的cid也為201;
截圖:
HTML效果:
完成后效果:
備注:PHP是服務(wù)器端的,建議發(fā)布網(wǎng)站后通過(guò)ip調(diào)試。
- AJAX省市區(qū)三級(jí)聯(lián)動(dòng)下拉菜單(java版)
- ajax三級(jí)聯(lián)動(dòng)下拉菜單效果
- jQuery ajax實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)
- ajax三級(jí)聯(lián)動(dòng)的實(shí)現(xiàn)方法
- jquery+ajax實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)效果簡(jiǎn)單示例
- jquery+ajax實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)(封裝和不封裝兩種方式)
- ajax實(shí)現(xiàn)無(wú)刷新省市縣三級(jí)聯(lián)動(dòng)
- ajax實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)的基本方法
- AJAX和WebService實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)具體代碼
- ajax實(shí)現(xiàn)城市三級(jí)聯(lián)動(dòng)
相關(guān)文章
Django中的cookie與session操作實(shí)例代碼
本文通過(guò)示例代碼給大家介紹了Django中的cookie與session操作,需要的朋友參考下吧2017-08-08PHP session文件獨(dú)占鎖引起阻塞問(wèn)題解決方法
這篇文章主要介紹了PHP session文件獨(dú)占鎖引起阻塞,本文講解PHP使用默認(rèn)文件會(huì)話處理器時(shí)容易導(dǎo)致的阻塞問(wèn)題解決方法,需要的朋友可以參考下2015-05-05使用Yii2實(shí)現(xiàn)主從數(shù)據(jù)庫(kù)設(shè)置
大家應(yīng)該都知道,當(dāng)項(xiàng)目做大了,數(shù)據(jù)庫(kù)主從還是不可少的。使用Yii框架開(kāi)發(fā),如何設(shè)置數(shù)據(jù)庫(kù)的主從呢?其實(shí)很簡(jiǎn)單。下面這篇文章就給大家詳細(xì)介紹了使用Yii2實(shí)現(xiàn)主從數(shù)據(jù)庫(kù)設(shè)置的方法,文中介紹的很詳細(xì),相信對(duì)大家的理解和學(xué)習(xí)很有幫助,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11新浪微博API開(kāi)發(fā)簡(jiǎn)介之用戶授權(quán)(PHP基礎(chǔ)篇)
己在開(kāi)發(fā)和學(xué)習(xí)的過(guò)程中,感覺(jué)雖然沒(méi)有太大難度,但還是有一些問(wèn)題是需要我們注意的,今天就我在開(kāi)發(fā)和學(xué)習(xí)的過(guò)程中,簡(jiǎn)單的對(duì)利用PHP進(jìn)行新浪微博API開(kāi)發(fā)的內(nèi)容進(jìn)行一個(gè)整理和說(shuō)明2011-09-09全世界最小的php網(wǎng)頁(yè)木馬一枚 附PHP木馬的防范方法
php網(wǎng)頁(yè)木馬代碼,大家可以看下自己的網(wǎng)站里面是不是有這樣的代碼,注意網(wǎng)站安全用mcafee限制w3wp.exe生成php或者asp文件。并在php.ini中設(shè)置一下。2009-10-10