基于asp+ajax和数据库驱动的二级联动菜单


Posted in PHP onMay 06, 2010

index.asp 页面代码

<!--#include file="conn.asp" --> 
<% 
set cmd = conn.execute("select bigclassid,bigclassname from bigclass") 
tempid=cmd("bigclassid") 
%> 
<select name="menu" onChange="getsubcategory(this.value);"> <% 
if not cmd.eof then 
do while not cmd.eof 
bigclassid= cmd("bigclassid") 
bigclassname = cmd("bigclassname") 
%> 
<option value="<%=bigclassid%>"><%=bigclassname%></option> 
<% 
cmd.movenext 
loop 
end if 
cmd.close 
set cmd = nothing 
%> 
</select> 
<div id="subclass"> 
<select name="submenu"> 
<% 
set cxd = conn.execute("select * from smallclass where bigclassid=" & tempid) 
if not cxd.eof then 
do while not cxd.eof 
smallclassid= cxd("smallclassid") 
smallclassname = cxd("smallclassname")%> 
<option value="<%=smallclassid%>"><%=smallclassname%></option> 
<% 
cxd.movenext 
loop 
cxd.close 
set cxd = nothing 
else 
html = "<select name='smallclassid'><option value='0' selected>暂无小类</option></select>" 
response.write html 
end if 
%> 
</select> 
</div>

ajax.js 代码
// JavaScript Document 
function createxmlhttp() 
{ 
xmlhttpobj = false; 
try{ 
xmlhttpobj = new XMLHttpRequest; 
}catch(e){ 
try{ 
xmlhttpobj=new ActiveXObject("MSXML2.XMLHTTP"); 
}catch(e2){ 
try{ 
xmlhttpobj=new ActiveXObject("Microsoft.XMLHTTP"); 
}catch(e3){ 
xmlhttpobj = false; 
} 
} 
} 
return xmlhttpobj; 
} function getsubcategory(bigclassid){ 
if(bigclassid==0){ 
document.getElementById("subclass").innerHTML="<select name='smallclassid'><option value='0' selected>选择二级分类</option></select>"; 
return; 
}; 
var xmlhttpobj = createxmlhttp(); 
if(xmlhttpobj){//如果创建对象xmlhttpobj成功 
xmlhttpobj.onreadystatechange=handle; 
xmlhttpobj.open('get',"getsubcategory.asp?bigclassid="+bigclassid+"&number="+Math.random(),true);//get方法 加个随机数。 

xmlhttpobj.send(null); 
} 
} 
function handle(){//客户端监控函数 
//if(xmlhttpobj.readystate==4){//服务器处理请求完成 
if(xmlhttpobj.status==200){ 
//alert('ok'); 
var html = xmlhttpobj.responseText;//获得返回值 
document.getElementById("subclass").innerHTML=html; 
}else{ 
document.getElementById("subclass").innerHTML="对不起,您请求的页面有问题..."; 
} 
//} 
//else{ 
//document.getElementById("subclass").innerHTML=xmlhttpobj.readystate;//服务器处理中 
//} 
//} 
}

getsubcategory.asp 代码
<%@language="vbscript" codepage="936"%> 
<!--#include file="conn.asp"--> 
<% 
response.charset="gb2312" 
bigclassid=safe(request.querystring("bigclassid")) 
if bigclassid<>"" then 
set re=new regexp 
re.ignorecase=true 
re.global=false 
re.pattern = "^[0-9]{1,3}$" 
if not re.test(bigclassid) then 
response.write "非法参数" 
response.end 
end if%> <%on error resume next 
set p = conn.execute("select * from smallclass where bigclassid=" & bigclassid) 
if err then 
err.clear 
response.write "查询出错" 
response.end 
end if 
if not p.eof then 
html = "<select name='select2'>"&vbnewline 
do while not p.eof 
html = html&"<option value='"&p("smallclassid")&"'>"&p("smallclassname")&"</option>"&vbnewline 
p.movenext 
loop 
html = html&"</select>" 
else 
html = "<select name='smallclassid'><option value='0' selected>暂无小类</option></select>" 
end if 
p.close 
set p = nothing 
conn.close 
set conn = nothing 
response.write html 
html = "" 
end if 
%>
PHP 相关文章推荐
杏林同学录(二)
Oct 09 PHP
phpMyAdmin下载、安装和使用入门教程
May 31 PHP
Zend Studio去除编辑器的语法警告设置方法
Oct 24 PHP
腾讯CMEM的PHP扩展编译安装方法
Sep 25 PHP
详解PHP执行定时任务的实现思路
Dec 21 PHP
PHP微信公众号自动发送红包API
Jun 01 PHP
Apache PHP MySql安装配置图文教程
Aug 27 PHP
PHP CURL post数据报错 failed creating formpost data
Oct 16 PHP
php简单实现文件或图片强制下载的方法
Dec 06 PHP
PHP DB 数据库连接类定义与用法示例
Mar 11 PHP
php实现简单的守护进程创建、开启与关闭操作
Aug 13 PHP
详解laravel passport OAuth2.0的4种模式
Nov 04 PHP
PHP 类商品秒杀计时实现代码
May 05 #PHP
PHP 面向对象 final类与final方法
May 05 #PHP
PHP 面向对象 PHP5 中的常量
May 05 #PHP
在Windows下编译适用于PHP 5.2.12及5.2.13的eAccelerator.dll(附下载)
May 04 #PHP
一些被忽视的PHP函数(简单整理)
Apr 30 #PHP
php 将字符串按大写字母分隔成字符串数组
Apr 30 #PHP
mayfish 数据入库验证代码
Apr 30 #PHP
You might like
php ob_flush,flush在ie中缓冲无效的解决方法
2010/05/09 PHP
PHP 获取指定地区的天气实例代码
2017/02/08 PHP
php readfile下载大文件失败的解决方法
2017/05/22 PHP
laravel通过a标签从视图向控制器实现传值
2019/10/15 PHP
JavaScript入门教程(9) Document文档对象
2009/01/31 Javascript
jQuery TextBox自动完成条
2009/07/22 Javascript
js获取TreeView控件选中节点的Text和Value值的方法
2012/11/24 Javascript
表单类各种类型(文本框)失去焦点效果jquery代码
2013/04/26 Javascript
javascript 中iframe高度自适应(同域)实例详解
2017/05/16 Javascript
微信小程序使用navigateTo数据传递的实例
2017/09/26 Javascript
微信小程序下拉框搜索功能的实现方法
2019/07/31 Javascript
基于JavaScript获取base64图片大小
2019/10/18 Javascript
解决微信小程序scroll-view组件无横向滚动的问题
2020/02/04 Javascript
[01:35]辉夜杯战队访谈宣传片—LGD
2015/12/25 DOTA
在Apache服务器上同时运行多个Django程序的方法
2015/07/22 Python
使用PyV8在Python爬虫中执行js代码
2017/02/16 Python
Python使用paramiko操作linux的方法讲解
2019/02/25 Python
python快排算法详解
2019/03/04 Python
详解Python中pandas的安装操作说明(傻瓜版)
2019/04/08 Python
python爬取基于m3u8协议的ts文件并合并
2019/04/26 Python
python opencv 二值化 计算白色像素点的实例
2019/07/03 Python
详解pyinstaller selenium python3 chrome打包问题
2019/10/18 Python
Python如何使用内置库matplotlib绘制折线图
2020/02/24 Python
Python根据URL地址下载文件并保存至对应目录的实现
2020/11/15 Python
来自南加州灵感的工作和娱乐服装:TravisMathew
2019/05/01 全球购物
小米官方旗舰店:Xiaomi
2020/08/07 全球购物
农村婚礼主持词
2014/03/13 职场文书
经典而简洁的婚礼主持词
2014/03/13 职场文书
个人查摆问题及整改措施
2014/10/16 职场文书
贵阳市党的群众路线教育实践活动党(工)委领导班子整改方案
2014/10/26 职场文书
先进教师事迹材料
2014/12/16 职场文书
《所见》教学反思
2016/02/23 职场文书
基于Redis位图实现用户签到功能
2021/05/08 Redis
浅谈CSS不规则边框的生成方案
2021/05/25 HTML / CSS
Java Spring 控制反转(IOC)容器详解
2021/10/05 Java/Android
Mysql如何查看是否使用到索引
2022/12/24 MySQL