当前位置:Gxlcms > PHP教程 > php+mysql采用ajax技术的省市地3级联动无刷新菜单源码_PHP教程

php+mysql采用ajax技术的省市地3级联动无刷新菜单源码_PHP教程

时间:2021-07-01 10:21:17 帮助过:11人阅读

很简单的

1.先把
加到 header.html 页
2.把 regionss() 函数 加到 /include/tag.php 里
3.在你网站的根目录建立个 regions.php 文件
4.在你的模板的适当的地方加入 {$regionss(中华人民共和国)} 即可

注意: 表单的名字可根据你的需要可以自己修改...


测试页代码:



php+ajax动态生成下拉菜单





regionss 函数代码:
//省市地3级联动下拉菜单函数 regionss 省.市.地
function regionss($country="中华人民共和国")
{
global $db; $i = 1;
$text="";
$text.=" \n";
$text.="\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 页面代码

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里直接使用,若在其他地方使用请自己修改

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/631911.htmlTechArticle很简单的 1.先把 script language=JavaScript src=../include/js/regions.js/script 加到 header.html 页 2.把 regionss() 函数 加到 /include/tag.php 里 3.在你网站的根目...

人气教程排行