A Functional Polling System - Page 4
January 8, 2001
Our Completed Voting System (well almost...)
Believe it or not, that is the most basic solution possible.
Here is a full look at the ASP code just in case something is not
clear.
<%
dim Conn, rs_Questions, rs_Responses
set Conn = Server.CreateObject("ADODB.Connection")
Conn.ConnectionString = "dsn=polldb;database=polldb"
Conn.ConnectionTimeout = 60
Conn.CommandTimeout = 60
Conn.Open
set rs_Questions = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT TOP 1 * FROM tbl_questions " & _
"ORDER BY PollDate DESC"
rs_Questions.open strSQL, Conn, 3, 3
recCount = rs_Questions.RecordCount
PollID = rs_Questions("PollID")
PollQuestion = rs_Questions("PollQuestion")
strSQL = "SELECT * FROM tbl_Responses " & _
"WHERE PollID = " & PollID
set rs_Responses = Server.CreateObject("ADODB.Recordset")
rs_Responses.Open strSQL, Conn, 3, 3
Total = 0
While Not rs_Responses.EOF
Total = Total + rs_Responses("VoteCount")
PollOptHTML = PollOptHTML & _
"<tr><td>" & rs_Responses("PollOption") & _
"</td><td><input type=""radio""" & _
"name=""pollopt"" ""value=""" & _
rs_Responses("PollOptionID") & """></td></tr>"
rs_Responses.MoveNext
Wend
If Request.QueryString("isSubmitted") = "yes" then
strSQL = "UPDATE tbl_Responses " & _
"SET VoteCount = VoteCount + 1 " & _
"WHERE PollOptionID= " & _
Request.QueryString("pollopt") & ";"
Conn.Execute strSQL
End if
%>
<html>
<head></head>
<body>
<form name="poll" action="poll.asp" method="get">
<table width="250">
<tr><td colpan="2">
<%= PollQuestion %>
</td></tr>
<%= PollOptHTML %>
<tr><td colspan="2">
<input type="submit" value="Submit Vote">
</td></tr>
</table>
<input type="hidden" name="isSubmitted" value="yes">
</form>
</body>
</html>
That was not too bad. It is a functional solution but it is
also a flawed one. Not to say it will not work but there are so
many things left to desire there that we should probably work on
it some more. Following are two important reasons this voting
system is not so great in its current form.
First, it is well and good to be able to submit a vote, but what
if you are dealing with a site where people are likely to want
their outcome to win (read: every site out there), what in the
code prevents a user from voting more then once? You got it,
there is nothing there.
Second, while many people like to interact, some people do not -
or after they do, they like to see how their vote impacted the
results to date. Nowhere in our code is any type of "View
Results" behavior. We need to figure out how to implement it in
a way that is helpful to users.
Move on to the next page and we will tackle the first problem
with some cookies! Yum!
The Birth of a Poll - Page 3
Poll Your Visitors with ASP
Making Cookies - Page 5
|