落伍首發 php+mysql 採用ajax技術的 省 市 地 3級聯動無重新整理菜單 源碼

來源:互聯網
上載者:User

絕對原創
測試地址: http://www.mlmm.cn/mypage/?name=ceshi

測試頁代碼:複製代碼 代碼如下:<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>php+ajax動態產生下拉式功能表</title>
<script language="JavaScript" src="../include/js/regions.js"></script>
</head>
<body>
<form name="form1" method="post" action="">
<tr><td>{$regionss(中華人民共和國)}</td></tr>
</form>
</body>
</html>

regionss 函數代碼:
QUOTE: 複製代碼 代碼如下://省市地3級聯動下拉式功能表函數 regionss 省.市.地
function regionss($country="中華人民共和國")
{
global $db; $i = 1;
$text="<select id=\"select1\" name=\"select1\" onchange=\"startRequest()\" style=\"width:90px;\"><option value=\"\">省/市/自治區</option>\n";

$result = $db->query("SELECT province FROM ".TABLE_PROVINCE." WHERE country='$country' ORDER BY provinceid");
while($r = $db->fetch_array($result))
{
$text .= "<option value=".$r['province'].">".$r['province']."</option> ";
$i++;
}
$text.="</select>";
$text.="&nbsp<select id=\"select2\" name=\"select2\" onchange=\"startRequesta()\" style=\"width:90px;\"><option value=\"\">市/縣/區</option></select>\n";
$text.="<select id=\"select3\" name=\"select3\" style=\"width:90px;\"><option value=\"\">縣級市/縣</option></select>\n";
return $text;

}

regions.js 的代碼:

複製代碼 代碼如下://var ab = new Array();
var xmlHttp;
var xmlHttpa;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function createXMLHttpRequesta() {
if (window.ActiveXObject) {
xmlHttpa = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttpa = new XMLHttpRequest();
}
}

function startRequest() {
createXMLHttpRequest();
createXMLHttpRequesta();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttpa.onreadystatechange = handleStateChangearea;
document.getElementById('select2').options.length = 0;
document.getElementById('select3').options.length = 0;
var url = document.form1.select1.value;
var qurl = "/regions.php?province="+url+"&time="+new Date().getTime();
xmlHttp.open("GET", qurl, true);
xmlHttp.send(null);
//setTimeout("startRequest()",2000);
}

function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
var obj = document.getElementById('select2'); //將伺服器返回的字串寫到頁面中ID為select2的地區
obja = document.getElementById('select3'); //將伺服器返回的字串寫到頁面中ID為select3的地區
eval(xmlHttp.responseText);

}
}
}
function handleStateChangearea() {
eval(xmlHttpa.responseText);
}
//var ab = new Array();

function startRequesta() {
createXMLHttpRequesta();
xmlHttpa.onreadystatechange = handleStateChangea;
document.getElementById('select3').options.length = 0;
var url = document.form1.select2.value;
var qurl = "/regions.php?city="+url+"&time="+new Date().getTime();
xmlHttpa.open("GET", qurl, true);
xmlHttpa.send(null);
//setTimeout("startRequest()",2000);
}

function handleStateChangea() {
if(xmlHttpa.readyState == 4) {
if(xmlHttpa.status == 200) {
var obja = document.getElementById('select3');
eval(xmlHttpa.responseText);
//將伺服器返回的字串寫到頁面中ID為select3的地區

}
}
}

資料島 regions.php 頁面代碼

複製代碼 代碼如下:<?php
require "common.php";
$city = $city ? $city : "";
$area = $area ? $area : "";
if($province && $city==''){
global $db; $i = 1;

$result = $db->query("SELECT DISTINCT city FROM ".TABLE_CITY." WHERE province='$province' ORDER BY cityid");
while($r = $db->fetch_array($result))
{
$r[city]=iconv('gb2312','UTF-8',$r[city]);
echo "obj.options[obj.options.length] = new Option('".$r[city]."','".$r[city]."');\n";
$i++;
}
$resultarea = $db->query("SELECT DISTINCT city FROM ".TABLE_CITY." WHERE province='$province' ORDER BY cityid");
$r = $db->fetch_array($resultarea);
$city = $r[city];
$resulta = $db->query("SELECT DISTINCT area FROM ".TABLE_CITY." WHERE city='$city' ORDER BY cityid");
while($ra = $db->fetch_array($resulta))
{
$ra[area]=iconv('gb2312','UTF-8',$ra[area]);
echo "obja.options[obja.options.length] = new Option('".$ra[area]."','".$ra[area]."');\n";
$i++;
}

}
if($city && $province==''){
global $db; $i = 1;

$result = $db->query("SELECT DISTINCT area FROM ".TABLE_CITY." WHERE city='$city' ORDER BY cityid");
while($r = $db->fetch_array($result))
{
$r[area]=iconv('gb2312','UTF-8',$r[area]);
echo "obja.options[obja.options.length] = new Option('".$r[area]."','".$r[area]."');\n";
$i++;
}
}
?>

地址資訊資料庫:

採用的是phpcms3.0裡內建的資料庫,我沒有進行任何改動

本程式可以在phpcms3.0裡直接使用,若在其他地方使用請自己修改

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.