基于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过滤危险html代码的函数
Jul 22 PHP
Zend Guard一些常见问题解答
Sep 11 PHP
PHP 远程关机实现代码
Nov 10 PHP
php MessagePack介绍
Oct 06 PHP
Linux下安装PHP MSSQL扩展教程
Oct 24 PHP
PHP基于CURL进行POST数据上传实例
Nov 10 PHP
PHP中Session可能会引起并发问题
Jun 26 PHP
PHP下使用mysqli的函数连接mysql出现warning: mysqli::real_connect(): (hy000/1040): ...
Feb 14 PHP
PHP使用php-resque库配合Redis实现MQ消息队列的教程
Jun 29 PHP
php加密解密字符串示例
Oct 13 PHP
Laravel实现表单提交
May 07 PHP
php实现将数据做成json的格式给前端使用
Aug 21 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 XML数据解析代码
2010/05/26 PHP
利用js的Node遍历找到repeater的一个字段实例介绍
2013/04/25 Javascript
返回上一页并自动刷新的JavaScript代码
2014/02/19 Javascript
javascript类型转换示例
2014/04/29 Javascript
基于jQuery和hwSlider实现内容左右滑动切换效果附源码下载(一)
2016/06/22 Javascript
javascript 小数乘法结果错误的处理方法
2016/07/28 Javascript
微信小程序开发的四十个技术窍门总结(推荐)
2017/01/23 Javascript
Jquery EasyUI $.Parser
2017/06/02 jQuery
vue单页开发父子组件传值思路详解
2018/05/18 Javascript
vue 中基于html5 drag drap的拖放效果案例分析
2018/11/01 Javascript
Vue表单控件绑定图文详解
2019/02/11 Javascript
重学 JS:为啥 await 不能用在 forEach 中详解
2019/04/15 Javascript
Python微信企业号开发之回调模式接收微信端客户端发送消息及被动返回消息示例
2017/08/21 Python
python使用sqlite3时游标使用方法
2018/03/13 Python
深入理解python中sort()与sorted()的区别
2018/08/29 Python
Django之Mode的外键自关联和引用未定义的Model方法
2018/12/15 Python
对PyQt5基本窗口控件 QMainWindow的使用详解
2019/06/19 Python
Python加密模块的hashlib,hmac模块使用解析
2020/01/02 Python
解决Tensorflow占用GPU显存问题
2020/02/03 Python
tf.concat中axis的含义与使用详解
2020/02/07 Python
Python处理PDF与CDF实例
2020/02/26 Python
Window系统下Python如何安装OpenCV库
2020/03/05 Python
keras导入weights方式
2020/06/12 Python
Pyqt助手安装PyQt5帮助文档过程图解
2020/11/20 Python
Pycharm创建文件时自动生成文件头注释(自定义设置作者日期)
2020/11/24 Python
Django中使用Celery的方法步骤
2020/12/07 Python
css3实现信纸/同学录效果的示例代码
2018/12/11 HTML / CSS
生物技术研究生自荐信
2013/11/12 职场文书
寄语十八大感言
2014/02/07 职场文书
党员干部群众路线个人整改措施
2014/09/18 职场文书
工程承包协议书范本
2014/09/29 职场文书
教师自我剖析材料(四风问题)
2014/09/30 职场文书
导游词怎么写
2015/02/04 职场文书
2015年财务科工作总结范文
2015/05/13 职场文书
焦裕禄纪念馆观后感
2015/06/09 职场文书
Netty分布式客户端处理接入事件handle源码解析
2022/03/25 Java/Android