天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

PHP+Mysql+Ajax+JS實現省市區三級聯動

基本思想就是:在JS動態創建select控件的option,通過Ajax獲取在php從SQL數據庫獲取的省市區信息,代碼有點長,但很多都是類似的,例如JS中省、市、區獲取方法類似,php中通過參數不同執行不同的select語句。

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>省市區三級聯動</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">請選擇省份</option>
</select>
<select onchange="sech(this.id)" id="shi">
<option value="city">請選擇市區</option>
</select>
<select id="xian">
<option value="county">請選擇縣鄉</option>
</select>
</div>

</div>
</body>
</html>

thumbnails.js代碼:

復制代碼 代碼如下:
window.onload = getProvince;

function createRequest() {//Ajax于php交互需要對象
  try {
    request = new XMLHttpRequest();//創建一個新的請求對象;
  } 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) {//省市改變時觸發,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時傳遞至php時讓其獲取所有省
  request.open("GET", url, true);
  request.onreadystatechange = displayProvince; //設置回調函數
  request.send(null);    //發送請求
}

function getCity(id){//獲取省對應的市
  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){//獲取市對應的區
  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() {//將獲取的數據動態增加至select
  if (request.readyState == 4) {
    if (request.status == 200) {
  var a=new Array;
var b=request.responseText;//將php返回的數據賦值給b
 a=b.split(",");//通過","將這一數據保存在數組a中
  document.getElementById("sheng").length=1;
  var obj=document.getElementById("sheng'); 
  for(i=0;i
      obj.options.add(new Option(a[i],i+1)); //動態生成OPTION加到select中,第一個參數為Text,第二個參數為Value值.

    }
  }
}

 
function displayCity() {//將獲取的數據動態增加至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對應的是市的ID。
}

    }
  }
}

function displayCounty() {//將獲取的數據增加至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); //建立數據庫連接
$sqlstr = "select name from Province"; //設置查詢字符串
$rs = $conn->Execute($sqlstr); //執行查詢獲得結果
$num_cols = $rs->Fields->Count(); //得到數據集列數
$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){//獲得省對應的市列表
$conn->Open($connstr); //建立數據庫連接
$sqlstr = "select name from City where cid=".$_REQUEST['ID']; //設置查詢字符串
$rs = $conn->Execute($sqlstr); //執行查詢獲得結果
$num_cols = $rs->Fields->Count(); //得到數據集列數
$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){//獲得省市對應的縣列表
$conn->Open($connstr); //建立數據庫連接
$sqlstr = "select name from County where cid=".$_REQUEST['ID']; //設置查詢字符串
$rs = $conn->Execute($sqlstr); //執行查詢獲得結果
$num_cols = $rs->Fields->Count(); //得到數據集列數
$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;
}
?>

數據庫設計,表格Province表,City表,County表。
要求:Province表需要id和name,id建議從1至34,例如北京id為1,廣東id為2,以此類推;
        City表需要id,name和cid,id為cid*100+1,cid為該市的上級,例如深圳的上級為廣東省,cid為2的話,深圳的id就是201,以此類推。
        County表需要id,name和cid,因為是三級的關系,id可以隨意,建議從10001開始自增。cid為所在上級,例如寶安區的cid為201,龍崗區的cid也為201;

截圖:

 HTML效果:

完成后效果:

備注:php服務器端的,建議發布網站后通過ip調試。

php技術PHP+Mysql+Ajax+JS實現省市區三級聯動,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 日韩毛片大全 | 亚洲日韩乱码人人爽人人澡人 | jizz丝袜| 国产精品永久免费 | beeg日本老师按摩 | nxgx69日本护士 | 视频一区二区中文字幕 | 在线电影一区二区 | 亚洲精品黄色 | 国语92电影网午夜福利 | 国产成人午夜精品免费视频 | 欧美日韩第一区 | 肉耽高h一受n攻 | 国产午夜精品AV一区二区麻豆 | 真实处破女全过程完免费观看 | 啦啦啦影院视频在线看高清... | 国产精品无码无卡毛片不卡视 | 天天影视色欲 影视 | 中文字幕永久在线 | 欧美色图一区二区三区 | 福利一区国产 | 9LPORM原创自拍达人 | 精品无码国产自产在线观看 | 精品国产美女AV久久久久 | 国精产品一区一区三区M | 真实处破女全过程完免费观看 | 亚洲精品久久久一区 | 国产精品18久久久久久欧美网址 | 99久久久无码国产精品免费人妻 | 国产噜噜噜精品免费 | 欧美亚洲日韩欧洲不卡 | 幺妹视频福利视频 | 久久国产乱子伦精品免费M 久久国产露脸老熟女熟69 | 亚洲国产无线码在线观看 | 午夜一级视频 | 肉耽高h一受n攻 | 成人国产在线观看 | 啪啪漫画无遮挡全彩h同人 啪啪激情婷婷久久婷婷色五月 | 欧美成 人 网 站 免费 | 性色爽爱性色爽爱网站 | 91免费永久在线地址 |