ajax三级联动+全国最新省市县数据
生活随笔
收集整理的這篇文章主要介紹了
ajax三级联动+全国最新省市县数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
寫個Ajax三級聯動實例,用的最新的全國省市縣mysql數據。這里只說一下思路,具體請看源碼。 首先看index.php,這是一個省份列表 <?php
??$conn=mysql_connect("localhost","root","");
??mysql_select_db("china");
??mysql_query("set names 'utf8'");
??$sql="select * from t_province order by ProSort ASC";
??$result=mysql_query($sql);
??echo "<from id='form1'>\n";
??echo "<select id='province' οnchange='queryCity(this.options[this.selectedIndex].value)'>\n";
??echo "<option value='-1' selected>請選擇省份</option>\n";
??while($row=mysql_fetch_row($result)){
????echo "<option value='$row[0]'>$row[1]</option>\n";
??}
??echo "</select>\n";
??echo "<span id='city'></span>\n";
??echo "<span id='area'></span>\n";
??echo "</form>\n";
?> 還有兩個javascript代碼作用是傳遞省份主鍵值和城市主鍵值到ajax.php中查詢數據。 function queryCity(province){
??createXMLHttpRequest();
??type="city";
??var url="ajax.php?type=province&province="+province;
??xmlHttp.open("GET",url,true);
??xmlHttp.onreadystatechange=handleStateChange;
??xmlHttp.send(null);
}
function queryArea(city){
??createXMLHttpRequest();
??type="area";
??var url="ajax.php?type=city&city="+city;
??xmlHttp.open("GET",url,true);
??xmlHttp.onreadystatechange=handleStateChange;
??xmlHttp.send(null);
} ajax.php文件就是接受傳遞過來的數據并查詢相關市縣。 // 根據傳遞的省份ID值查詢t_city表中ProID值為該省份ID的城市。
if($_GET['type']=='province'){
??if(!empty($_GET['province'])){
????????????????????????????????$province = intval($_GET['province']);
????$sql="select * from `t_city` where `ProID` = '$province' ";
????$result=mysql_query($sql);
????echo "<select οnchange='queryArea(this.options[this.selectedIndex].value)'>\n";
????echo "<option value='-1' selected>請選擇城市</option>\n";
????while($row=mysql_fetch_row($result)){
??????echo "<option value='$row[0]'>$row[1]</option>\n";
????}
????echo "</select>\n";
??}
}
// 根據傳遞的城市ID值查詢t_district表中CityID值為該市ID的城市。
if($_GET['type']=='city'){
??if(!empty($_GET['city'])){
????????????????????????????????$city = intval($_GET['city']);
????$sql="select * from `t_district` where `CityID` = '$city'";
????$result=mysql_query($sql);
????echo "<select>\n";
????echo "<option value='-1' selected>請選擇縣</option>\n";
????while($row=mysql_fetch_row($result)){
??????echo "<option value='$row[0]'>$row[1]</option>\n";
????}
????echo "</select>\n";
??}
}
??$conn=mysql_connect("localhost","root","");
??mysql_select_db("china");
??mysql_query("set names 'utf8'");
??$sql="select * from t_province order by ProSort ASC";
??$result=mysql_query($sql);
??echo "<from id='form1'>\n";
??echo "<select id='province' οnchange='queryCity(this.options[this.selectedIndex].value)'>\n";
??echo "<option value='-1' selected>請選擇省份</option>\n";
??while($row=mysql_fetch_row($result)){
????echo "<option value='$row[0]'>$row[1]</option>\n";
??}
??echo "</select>\n";
??echo "<span id='city'></span>\n";
??echo "<span id='area'></span>\n";
??echo "</form>\n";
?> 還有兩個javascript代碼作用是傳遞省份主鍵值和城市主鍵值到ajax.php中查詢數據。 function queryCity(province){
??createXMLHttpRequest();
??type="city";
??var url="ajax.php?type=province&province="+province;
??xmlHttp.open("GET",url,true);
??xmlHttp.onreadystatechange=handleStateChange;
??xmlHttp.send(null);
}
function queryArea(city){
??createXMLHttpRequest();
??type="area";
??var url="ajax.php?type=city&city="+city;
??xmlHttp.open("GET",url,true);
??xmlHttp.onreadystatechange=handleStateChange;
??xmlHttp.send(null);
} ajax.php文件就是接受傳遞過來的數據并查詢相關市縣。 // 根據傳遞的省份ID值查詢t_city表中ProID值為該省份ID的城市。
if($_GET['type']=='province'){
??if(!empty($_GET['province'])){
????????????????????????????????$province = intval($_GET['province']);
????$sql="select * from `t_city` where `ProID` = '$province' ";
????$result=mysql_query($sql);
????echo "<select οnchange='queryArea(this.options[this.selectedIndex].value)'>\n";
????echo "<option value='-1' selected>請選擇城市</option>\n";
????while($row=mysql_fetch_row($result)){
??????echo "<option value='$row[0]'>$row[1]</option>\n";
????}
????echo "</select>\n";
??}
}
// 根據傳遞的城市ID值查詢t_district表中CityID值為該市ID的城市。
if($_GET['type']=='city'){
??if(!empty($_GET['city'])){
????????????????????????????????$city = intval($_GET['city']);
????$sql="select * from `t_district` where `CityID` = '$city'";
????$result=mysql_query($sql);
????echo "<select>\n";
????echo "<option value='-1' selected>請選擇縣</option>\n";
????while($row=mysql_fetch_row($result)){
??????echo "<option value='$row[0]'>$row[1]</option>\n";
????}
????echo "</select>\n";
??}
}
轉載于:https://blog.51cto.com/leven/330406
總結
以上是生活随笔為你收集整理的ajax三级联动+全国最新省市县数据的全部內容,希望文章能夠幫你解決所遇到的問題。