分享
 
 
 

用ASP学做一个在线调查(2)

王朝asp·作者佚名  2008-05-21
窄屏简体版  字體: |||超大  

六、代码编写

做好了流程设计后,写代码就比较有条理了。让我们从简单的开始。在编写代码之前,我们要先在数据库里输入一些记录,以便做测试。先加入一条调查问题,和几个调查答案,并手工输入一些统计信息。

我们先来写显示调查表单的surveycode.asp 这个文件要在其它页面中被调用,所以我们写成JS和VBS混用的方式。调用的时候可以把它放在某个表格中,用下面的语句:

<SCRIPT Language="JavaScript" SRC="surveycode.asp?id=1"></SCRIPT>

按照上面的流程,在显示表单前,先要判断一下调查是否存在,是否在进行中。另外,在表单中要提交一个隐藏的参数,来表示调查的问题编号(id),答案提交的时候,提交的是答案的编号vote_no

文件名 surveycode.asp

<!--#include file="inc.asp" --><%id=request.querystring("id")if id<>"" then ''如果有参数opendb my ''联接数据库sql="select * from survey where survey_id="& id ''查询语句searchtable my,sql,rs ''查询数据库if not rs.eof then ''如果有这个调查记录question=rs("survey_question") ''读出问题surveytype=rs("survey_type") ''读出答案类型stime=rs("survey_stime") ''读出开始时间etime=rs("survey_etime") ''读出结束时间closetable rs ''关闭表if stime<now() and etime>now() then ''如果调查正在进行中''下面输出调查表单''先输出表单和问题,表单提交到survey_vote.asp%>document.write("<form action=''survey_vote.asp'' target=''_blank'' method=''post''>");document.write("<table border=''1'' cellpadding=''2'' cellspacing=0'' bordercolorligh=''#000000''");document.write(" bordercolordark=''#ffffff'' width=''100%'' align=''center''><tbody>");document.write("<tr><td colspan=''2'' align=''center''><b><%=server.htmlencode(question)%></b></td></tr>");<%sql="select vote_no,vote_answer from survey_vote where vote_id="&id ''查询答案的SQLsearchtable my,sql,rs ''执行查询if not rs.eof then ''如果有答案,就输出for i=1 to rs.recordcount%>document.write("<tr><td align=''right''><input name=''res'' type=''");<%if surveytype then ''判断类型,显示单选或者多选%>document.write("checkbox");<%else%>document.write("radio");<%end if ''下面这句输出答案的文字和提交的值(vote_no)%>document.write("'' value=<%=rs("vote_no")%>></td><td><%=rs("vote_answer")%></td></tr>");<%rs.movenextnext''下面几句输出一个隐藏的参数,传递问题编号(id)''并用一个JS函数来定义点击查看后的链接%>document.write("<tr><td colspan=''2'' align=''center''><input type=''hidden'' name=''id'' value=''<%=id%>''>");document.write("<input type=''submit'' class=button value=''投票''> ");document.write("<input type=button class=button value=''查看'' onclick=''jump(<%=id%>)''>");document.write("</td></tr></tbody></table></form>");function jump(id){window.open("survey_vote.asp?id="+id,"survey")}<%end ifend ifend ifclosetable rsclosedb myend if%>

在surveycode.asp完成后,我们实现上已经确定了以下几点:

1、在survey_vote.asp中,如果querystring参数id有值,则是查看结果;

2、在survey_vote.asp中,如果form参数id有值,则要先进行统计;

3、在survey_vote.asp中,提交来的form参数res是答案的编号vote_no;

七、统计结果

首先我们来完成与surveycode.asp最密切相关的显示统计结果survey_vote.asp文件。在上一篇的结尾,我们已经说明了在surveycode.asp中确定的一些参数。

统计结果 survey_vote.asp

<!--#include file="inc.asp" --><html><head><title>调查统计结果</title><link rel="stylesheet" href="main.css" type="text/css"></head><body><%''上一句先加入包含文件,引用函数。id=request.querystring("id") ''获取querystring参数idopendb my ''连接数据库if id="" then ''如果没有,则不是直接看结果id=request.form("id") ''获取form参数idif id<>"" then ''如果有值,则是要先统计surveycount() ''调用统计子程序end ifend ifif id<>"" thendisp_survey() ''不管是哪种,最后都显示结果end ifclosedb my ''关闭数据库''-----统计子程序-----sub surveycount()if session("survey_ok")="" then ''如果还没投票no=request.form("res") ''得到答案的编号if no<>"" then''定义SQL语句,让提交的答案数量+1sql="update survey_vote set vote_count=vote_count+1 where vote_no= in (" & no &")"my.execute sqlend ifsession("survey_ok")="ok"end ifend sub''------------------''---显示结果子程序---sub disp_survey()''定义SQL语句,得到调查的问题sql="select survey_question from survey where survey_id=" & idsearchtable my,sql,rs ''执行查询question=rs("survey_question") ''把问题存到question中closetable rs ''关闭表''定义SQL语句,得到答案的数量总和sql="select sum(vote_count) as total from survey_vote where vote_id="& idsearchtable my,sql,rstotal=rs("total")closetable rs ''关闭表''定义SQL语句,得到所有的答案文本部份及投票数sql="select vote_answer,vote_count from survey_vote where vote_id=" & idsearchtable my,sql,rs ''执行查询''下面用表格来输出统计表%><table width="500" border="1" align="center" cellpadding="2" cellspacing="0"bordercolorligh="#000000" bordercolordark="#ffffff"><tr><td colspan="4" align="center"><b>调查统计结果</b></td></tr><tr><td colspan="4"><b>调查问题:<%=question%></b></td></tr><tr ><td width="150" align="center" height="20">答案</td><td width="150" align="center" height="20">投票率</td><td width="100" align="center" height="20">比例</td><td width="100" align="center" height="20">票数</td></tr><%do while not rs.eofif total=0 thenpercent=0 ''如果没人投票,则百分比为0elsepercent=int(rs("vote_count")/total*10000)/100 ''计算百分比end if%><tr><td width="150" align="center"><%=rs("vote_answer")%></td><td width="150" align="left"><table border="0" width="<%=percent%>" bgcolor="#CCCC00" height="10"><tr><td></td></tr></table></td><td width="100" align="center"><%=percent%>%</td><td width="100" align="center"><%=rs("vote_count")%></td></tr><%rs.movenextloop%><tr><td colspan="4"> 至 <%=now()%> 止,共有 <%=total%> 张投票<a href="javascript:window.close()">关闭窗口</a></td></tr></table><%closetable rs ''关闭表end sub''------------------%></body></html>

在显示投票过程中,我们用session变量survey_ok来表示是否已经投过票。另外,这显示统计中,引用CSS文件来控制表格的样式,你们可以根据自己的要求自己加入。

八、列出所有调查的状态

现在我们来完成survey.asp,它的主要任务是列出所有的调查状态,包括:

1、调查的问题,链接到投票表单页面(直接写在本页中);

2、调查的起启时间;

3、调查的结束时间;

4、调查的进行状态:未开始、进行中、已结束;

5、调查的投票数;

6、调查的类型,单选还是多选;

7、另外给出一个链接查看投票结果;

根据这些要求,查询相应的表就可以了,有些语句,比如得到投票总数,SQL语句其实在上面的survey_vote.asp中已经写过了。

列出所有调查的状态 survey.asp

<!--#include file="inc.asp" --><html><head><title>在线调查列表</title><link rel="stylesheet" href="main.css" type="text/css"></head><body><%id=request.querystring("id") ''获取参数if id<>"" then ''如果有参数,则显示这个调查表单response.write "<SCRIPT Language=''JavaScript'' SRC=''surveycode.asp?id="&id&"''></SCRIPT>"else ''否则调用子程序显示状态disstat()end if''-----显示状态子程序----sub disstat()opendb my ''连接数据库opentable my,"survey",rs ''直接打开表''下面用表格显示每个记录''先显示表头%><table width="760" border="1" cellspacing="0" cellpadding="2"align="center" bordercolorligh="#000000" bordercolordark="#ffffff"><tr><td colspan="8" align="center"><b>在线调查列表</b></td></tr><tr ><td width="50" align="center" height="20">编号</td><td width="200" align="center" height="20">调查问题</td><td width="50" align="center" height="20">类型</td><td width="140" align="center" height="20">起启时间</td><td width="140" align="center" height="20">结束时间</td><td width="50" align="center" height="20">状态</td><td width="80" align="center" height="20">已投票数</td><td width="50" align="center" height="20">查看</td></tr><%''下面输出每个记录do while not rs.eof''先读出每个字段id=rs("survey_id")question=rs("survey_question")''读出类型if rs("survey_type") thenstype="多选"elsestype="单选"end ifstime=rs("survey_stime")etime=rs("survey_etime")''判断状态if now()<stime thenstat="未开始"elseif now<etime thenstat="进行中"elsestat="已结束"end ifend if''定义SQL语句,得到答案的数量总和sql="select sum(vote_count) as total from survey_vote where vote_id="& idsearchtable my,sql,tmprs ''查询total=tmprs("total")closetable tmprs ''关闭表''下面输出一条记录%><tr ><td align="center" height="20"><%=id%></td><td height="20"><a href="survey.asp?id=<%=id%>"><%=question%></a></td><td align="center" height="20"><%=stype%></td><td align="center" height="20"><%=stime%></td><td align="center" height="20"><%=etime%></td><td align="center" height="20"><%=stat%></td><td align="center" height="20"><%=total%></td><td align="center" height="20"><a href="survey_vote.asp?id=<%=id%>" target="_blank">查看</a></td></tr><%rs.movenext ''移动到下一条,循环loop%></table><%closetable rs ''关闭表closedb my ''关闭数据库end sub''----------------------%></body></html>九、后台管理

在后台管理页面survey_manage.asp中,前面我们已经列出来它所要实现的管理功能。管理的流程是先显示出所有调查,对于还没有开始的调查,可以进行修改、删除;对于已经结束的调查,可以删除,不能修改;对于正在进行的调查,只能修改它的结束时间。用一个参数action来表示动作,含义如下:

1、无参数。表示第一次进入,显示登录表单

2、login 表示执行登录

3、logout 表示执行退出登录

4、showaddquestion 表示显示增加一个调查

5、showsurvey 表示显示一个调查

6、doaddsurvey 表示执行增加一个调查

7、doaddanswer 表示执行增加一个答案

8、dodelsurvey 表示删除一个调查

9、dodelanswer 表示删除一个答案

10、domodify 表示修改一个调查及答案

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有