一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - PHP+Mysql+Ajax+JS實現(xiàn)省市區(qū)三級聯(lián)動

PHP+Mysql+Ajax+JS實現(xiàn)省市區(qū)三級聯(lián)動

2020-06-30 12:07PHP實例教程 PHP教程

最近做了個項目,需要用到省市區(qū)三級聯(lián)動,上網翻了不少資料,于是有了下面的思路和代碼

基本思想就是:在JS動態(tài)創(chuàng)建select控件的option,通過Ajax獲取在PHP從SQL數(shù)據(jù)庫獲取的省市區(qū)信息,代碼有點長,但很多都是類似的,例如JS中省、市、區(qū)獲取方法類似,PHP中通過參數(shù)不同執(zhí)行不同的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>省市區(qū)三級聯(lián)動</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">請選擇市區(qū)</option>
</select>
<select id="xian">
<option value="county">請選擇縣鄉(xiāng)</option>
</select>
</div>

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

 

thumbnails.js代碼:

 

復制代碼 代碼如下:

window.onload = getProvince;

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

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){//獲取市對應的區(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ù)動態(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(",");//通過","將這一數(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)); //動態(tài)生成OPTION加到select中,第一個參數(shù)為Text,第二個參數(shù)為Value值.

    }
  }
}

 
function displayCity() {//將獲取的數(shù)據(jù)動態(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對應的是市的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ù)庫連接
$sqlstr = "select name from Province"; //設置查詢字符串
$rs = $conn->Execute($sqlstr); //執(zhí)行查詢獲得結果
$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){//獲得省對應的市列表
$conn->Open($connstr); //建立數(shù)據(jù)庫連接
$sqlstr = "select name from City where cid=".$_REQUEST['ID']; //設置查詢字符串
$rs = $conn->Execute($sqlstr); //執(zhí)行查詢獲得結果
$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){//獲得省市對應的縣列表
$conn->Open($connstr); //建立數(shù)據(jù)庫連接
$sqlstr = "select name from County where cid=".$_REQUEST['ID']; //設置查詢字符串
$rs = $conn->Execute($sqlstr); //執(zhí)行查詢獲得結果
$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ù)庫設計,表格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為所在上級,例如寶安區(qū)的cid為201,龍崗區(qū)的cid也為201;

截圖:

 HTML效果:

PHP+Mysql+Ajax+JS實現(xiàn)省市區(qū)三級聯(lián)動

完成后效果:

PHP+Mysql+Ajax+JS實現(xiàn)省市區(qū)三級聯(lián)動

PHP+Mysql+Ajax+JS實現(xiàn)省市區(qū)三級聯(lián)動

PHP+Mysql+Ajax+JS實現(xiàn)省市區(qū)三級聯(lián)動

備注:PHP是服務器端的,建議發(fā)布網站后通過ip調試。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: bl超h 高h 污肉快穿np | 涩情主播在线翻车 | 饭冈加奈子黑人解禁在线播放 | 日韩小视频在线观看 | 欧洲vodafonewi精品 | 高h全肉np触手| 春意影院午夜爽爽爽免费 | 欧美猛男同志同性video | 999久久免费高清热精品 | 亚洲精品久久久成人 | 女教师巨大乳孔中文字幕免费 | 亚洲视频中文 | 2020韩国r级理论片在线观看 | 美女逼逼喷水 | 欧美成人一区二区三区 | 草久久网 | 色人阁导航 | 被强迫调教的高辣小说 | 女人用粗大自熨喷水在线视频 | 2019午夜福合集高清完整版 | 动漫美女羞羞视频 | 先锋影音 av | 91视频破解版 | 四虎影视4hu最新地址在线884 | 2021国产麻豆剧传媒剧情最新 | 羞羞漫画视频 | s8017加密路线免费 | 男女男在线精品网站免费观看 | 国产综合第一页 | 国产精品福利 | 变态女王麻麻小说在线阅读 | 15一16japanese破| 好大好硬好深好爽gif图 | 男女姓交大视频免费观看 | 高清免费毛片 | 亚洲欧美精品久久 | 精品国产一级毛片大全 | 国产黄色大片网站 | 日本私人影院 | a男人天堂| 美女的让男人桶爽网站 |