基于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 相关文章推荐
使用PHP提取视频网站页面中的FLASH地址的代码
Apr 17 PHP
PHP中用正则表达式清除字符串的空白
Jan 17 PHP
PHP通过正则表达式下载图片到本地的实现代码
Sep 19 PHP
PHP下打开phpMyAdmin出现403错误的问题解决方法
May 23 PHP
如何利用php array_multisort函数 对数据库结果进行复杂排序
Jun 08 PHP
PHP开发微信支付的代码分享
May 25 PHP
php中mkdir函数用法实例分析
Nov 15 PHP
浅谈ThinkPHP的URL重写
Nov 25 PHP
PHP实现检测客户端是否使用代理服务器及其匿名级别
Jan 07 PHP
php源码分析之DZX1.5加密解密函数authcode用法
Jun 17 PHP
Laravel学习教程之本地化模块
Aug 18 PHP
PHP+redis实现微博的推模型案例分析
Jul 10 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
Zerg兵种介绍
2020/03/14 星际争霸
用php+javascript实现二级级联菜单的制作
2008/05/06 PHP
关于PHP内存溢出问题的解决方法
2013/06/25 PHP
PHP扩展开发入门教程
2015/02/26 PHP
PHP aes (ecb)解密后乱码问题
2015/06/22 PHP
php简单生成一组与多组随机字符串的方法
2017/05/09 PHP
jquery UI 1.72 之datepicker
2009/12/29 Javascript
jQuery新闻滚动插件 jquery.roller.js
2011/06/27 Javascript
理解JSON:3分钟课程
2011/10/28 Javascript
复制js对象方法(详解)
2013/07/08 Javascript
jquery获取URL中参数解决中文乱码问题的两种方法
2013/12/18 Javascript
深入理解JavaScript系列(30):设计模式之外观模式详解
2015/03/03 Javascript
BootStrap中Table分页插件使用详解
2016/10/09 Javascript
mvc 、bootstrap 结合分布式图简单实现分页
2016/10/10 Javascript
Bootstrap DateTime Picker日历控件简单应用
2017/03/25 Javascript
JS字符串统计操作示例【遍历,截取,输出,计算】
2017/03/27 Javascript
让mocha支持ES6模块的方法实现
2020/01/14 Javascript
javascript单张多张图无缝滚动实例代码
2020/05/10 Javascript
Nuxt 项目性能优化调研分析
2020/11/07 Javascript
利用python批量给云主机配置安全组的方法教程
2017/06/21 Python
Python扩展内置类型详解
2018/03/26 Python
Python绘制正余弦函数图像的方法
2018/08/28 Python
详解Python字典小结
2018/10/20 Python
python for循环输入一个矩阵的实例
2018/11/14 Python
Python 制作查询商品历史价格的小工具
2020/10/20 Python
python RSA加密的示例
2020/12/09 Python
美国派对用品及装饰品网上商店:Shindigz
2016/07/30 全球购物
JAVA中的关键字有什么特点
2014/03/07 面试题
成功经营餐厅的创业计划书范文
2013/12/26 职场文书
高中军训感言500字
2014/02/24 职场文书
实验室的标语
2014/06/20 职场文书
党员民主评议总结
2014/10/20 职场文书
社区学习党的群众路线教育实践活动心得体会
2014/11/03 职场文书
2015年党风廉政建设责任书
2015/01/29 职场文书
谢师宴学生致辞
2015/07/27 职场文书
Java 关于String字符串原理上的问题
2022/04/07 Java/Android