欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

创建一个Web投票系统

程序员文章站 2022-07-02 15:35:39
下面zip文件:dir.co.uk/files/article-11.zip>https://www.content.aspdir.co.uk/files/article-11.zipduring...
下面zip文件:dir.co.uk/files/article-11.zip>https://www.content.aspdir.co.uk/files/article-11.zip

during this article you will learn how to construct your own web poll using asp. the article presumes you
already understand basic database interaction.



the following samples of code allow a user to select one of four options to a question. the users vote is
then recorded in a database and the user is taken to a page where the results for each option can be
viewed in statistical, numerical and graphical form. not bad, huh?

the whole application is based on the database itself, so when the values within the database are altered,
the application automatically changes. the database design itself, is not included within this article so
make sure to download a copy of the entire application, including the database before running the code.

the code for the first page is shown as follows: -

page: default.asp

<%
connects to database using recordset method
function dataconn(database,connection,recordset)
    set connection = server.createobject("adodb.connection")
    set recordset = server.createobject("adodb.recordset")
    connection.open "dbq=" & server.mappath(database) & ";driver={microsoft access(小型网站之最爱) driver (*.mdb)};"
end function
%>
<html>
<head>
    <title>poll example</title>
</head>

<body>
    <form name="languages" method=post action="pollresults.asp">
    <p>what is your favoutrite language?</p>
<%
calls dataconn function to open dbpoll.mdb
dataconn "files/dbpoll.mdb",podc,lars

selects all fields within tbllanguages
lars.open "select * from tbllanguages", podc

loop through and display each record within the database as a radio button
do while not lars.eof
    response.write lars("language") & ": <input type=radio name=language value=" & lars("language")
& "><br>"
    lars.movenext
loop

close database connection
lars.close
podc.close
set podc = nothing
%>
    <a href="pollresults.asp">view poll</a>
    <input type=submit value="vote">
    </form>
</body>
</html>


once connected to the database the script loops through each record, and displays that option as a radio
button. when the vote button is pressed, the inpidual value of the selected radio button is submitted
to the next page.

the code for the next page is shown below: -

page: pollresults.asp

<%
define all variables that will be used
dim i, percent

connects to database using recordset method
function dataconn(database,connection,recordset)
    set connection = server.createobject("adodb.connection")
    set recordset = server.createobject("adodb.recordset")
    connection.open "dbq=" & server.mappath(database) & ";driver={microsoft access(小型网站之最爱) driver (*.mdb)};"