落伍首发 php+mysql 采用ajax技术的 省 市 地 3级联动无刷新菜单 源码
程序员文章站
2022-09-20 11:13:31
绝对原创 测试地址: http://www.mlmm.cn/mypage/?name=ceshi 测试页代码:复制代码 代码如下:<...
绝对原创
测试地址: 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:
regions.js 的代码:
数据岛 regions.php 页面代码
地址信息数据库:
采用的是phpcms3.0里自带的数据库,我没有进行任何改动
本程序可以在phpcms3.0里直接使用,若在其他地方使用请自己修改
测试地址: 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.=" <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;
}
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.=" <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的区域
}
}
}
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++;
}
}
?>
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里直接使用,若在其他地方使用请自己修改
上一篇: 万网并入阿里云:传统主机托管被取代