基于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 相关文章推荐
回答PHPCHINA上的几个问题:URL映射
Feb 14 PHP
不错的一篇面向对象的PHP开发模式(简写版)
Mar 15 PHP
php mysql数据库操作分页类
Jun 04 PHP
攻克CakePHP系列一 连接MySQL数据库
Oct 22 PHP
php开发过程中关于继承的使用方法分享
Jun 17 PHP
初学PHP的朋友 经常问的一些问题。不断更新
Aug 11 PHP
php set_time_limit()函数的使用详解
Jun 05 PHP
Yii2 rbac权限控制操作步骤实例教程
Apr 29 PHP
屏蔽PHP默认设置中的Notice警告的方法
May 20 PHP
php合并数组并保留键值的实现方法
Mar 12 PHP
使用laravel的Eloquent模型如何获取数据库的指定列
Oct 17 PHP
Centos7安装swoole扩展操作示例
Mar 26 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 Session存储到Redis的方法
2013/11/04 PHP
PHP设计模式之模板方法模式定义与用法详解
2018/04/02 PHP
FormValidate 表单验证功能代码更新并提供下载
2008/08/23 Javascript
FF IE兼容性的修改小结
2009/09/02 Javascript
javascript中定义私有方法说明(private method)
2014/01/27 Javascript
js实现微博发布小功能
2017/01/12 Javascript
JavaScript在form表单中使用button按钮实现submit提交方法
2017/01/23 Javascript
浅谈js中用$(#ID)来作为选择器的问题(id重复的时候)
2017/02/14 Javascript
微信小程序基于slider组件动态修改标签透明度的方法示例
2017/12/04 Javascript
vue实现井字棋游戏
2020/09/29 Javascript
使用python解析xml成对应的html示例分享
2014/04/02 Python
Python 自动补全(vim)
2014/11/30 Python
在Python中使用PIL模块对图片进行高斯模糊处理的教程
2015/05/05 Python
python中的错误处理
2016/04/10 Python
Python排序搜索基本算法之选择排序实例分析
2017/12/09 Python
python实现判断一个字符串是否是合法IP地址的示例
2018/06/04 Python
解决Python print 输出文本显示 gbk 编码错误问题
2018/07/13 Python
python3.4爬虫demo
2019/01/22 Python
Python 数据库操作 SQLAlchemy的示例代码
2019/02/18 Python
Python使用pandas和xlsxwriter读写xlsx文件的方法示例
2019/04/09 Python
Python button选取本地图片并显示的实例
2019/06/13 Python
python计算波峰波谷值的方法(极值点)
2020/02/18 Python
Python 窗体(tkinter)下拉列表框(Combobox)实例
2020/03/04 Python
基于python SMTP实现自动发送邮件教程解析
2020/06/02 Python
Python使用plt.boxplot() 参数绘制箱线图
2020/06/04 Python
python切割图片的示例
2020/11/12 Python
中国电子产品外贸网站:MiniIntheBox
2017/02/06 全球购物
英国电子产品购物网站:Tech in the basket
2019/11/08 全球购物
英语自我评价范文
2014/01/24 职场文书
《雪地里的小画家》教学反思
2014/02/22 职场文书
会计电算化专业自荐信
2014/03/15 职场文书
党的群众路线教育实践方案
2014/05/11 职场文书
六一儿童节演讲稿
2014/05/23 职场文书
行政求职信
2014/07/04 职场文书
Python基础之教你怎么在M1系统上使用pandas
2021/05/08 Python
解决flex布局中子项目尺寸不受flex-shrink限制
2022/05/11 HTML / CSS