首页 / 网页编程 / ASP / 用ASP学做一个在线调查(2)
用ASP学做一个在线调查(2)2010-08-17六、代码编写做好了流程设计后,写代码就比较有条理了。让我们从 简单的开始。在编写代码
之前,我们要先在数据库里输入一些记录,以便 做测试。先加入一条调查问题,和几个
调查答案,并手工输入一些统计信 息。我们先来写显示调查表单的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 ""查询答案的SQL
searchtable 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.movenext
next
""下面几句输出一个隐藏的参数,传递 问题编号(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 if
end if
end if
closetable rs
closedb my
end if
%>
在surveycode.asp完成后,我们实现上 已经确定了以下几点:1、在survey_vote.asp中,如果querystring参数 id有值,则是查看结果;2、在survey_vote.asp中,如果form参数id有值 ,则要先进行统计;3、在survey_vote.asp中,提交来的form参数res是 答案的编号vote_no;