Visual Basic
Python
Web Scripting
Java
Online Tools


Home


What's New

Articles

Code Downloads

Code Snippets

Message boards
Links
Tool Box

Books

Mailing List
Receive free code snippets and notices when this site is updated.

Contact

WebPolls in ASP

Poll's can be a good way to get an idea of who is visiting your website and their collective thoughts on issues. They can be used just for fun or to help if your betting on the outcome of the next general election :-).

The WebPolls used on this site and explained in this article where created using an ASP and an access database. The polls are simple multi-choice ones.

Database Schema

The database schema for the poll application is shown below:

Every poll can have multiple PollOptions associated with it.  The PollOptions table holds the name of the option (e.g. "Yes", "No", etc) and the number of times that option has been chosen in the OptionSelectedCount field.

This Web application is made up of the files:

  • polls.asp - Lists all the polls in the database
  • poll.asp - Displays a given poll and lets the user vote if they haven't yet.
  • poll.mdb - Access database that holds the poll data.

The Polls Page (polls.asp)

The active server page "polls.asp" lists all the polls in the database by selecting all the records in the "Polls" table and listing them from the most recent poll to the earliest.

polls.asp

<HTML>
<HEAD><TITLE>All Polls</TITLE></HEAD>
<BODY>
<%
  Set objConn = CreateObject("ADODB.Connection")
  objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
  Server.MapPath("db\polls.mdb"))
  Set rsPolls = objConn.Execute("SELECT * FROM Polls ORDER BY PollDate DESC")

  Do While Not rsPolls.EOF
    Response.write "<P><A href=""poll.asp?PollID=" & rsPolls("PollID") & """>" 
    Response.write rsPolls("PollQuestion") & " - " & rsPolls("PollDate") & "</A></P>"
    rsPolls.MoveNext
  Loop
  ' Release memory
  rsPolls.close
  set rsPolls = Nothing
  objConn.close
  set objConn = Nothing
%>
<BODY>
</HTML>

For each poll a link it created that points to "poll.asp?PollID=<<Insert PollID Here>>".  The PollID attribute gives poll.asp the information it needs to look up the options for the given poll in the database.

A Poll (poll.asp)

This page displays the options for a poll, the total number of votes for the poll and which percentage of voters voted for each option.  If the user has not yet voted vote links are displayed so they can vote for an option.  

Weather the user has voted before is stored in a cookie  to make it more difficult for someone to vote twice.

Below is the code for poll.asp.  I have divided it into sections.

1
2
3
4
5
6
7
8
<% Response.Buffer = True %>
<HTML><HEAD><TITLE>A Poll</TITLE></HEAD>
<BODY>
<% PollID = Request.QueryString("PollID")
   If Len(PollID) <> 0 Then ' Check to see if a PollID was given
     ' Lookup poll id in database
     Set objConn = CreateObject("ADODB.Connection")
     objConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
                  Server.MapPath("db\polls.mdb"))

Line 1 turns buffering on.  This stop anything being sent to the browser until the entire page is done.  This is necessary because this script uses cookies and cookies must be set before any data is sent.

Line 4 and 5 check to see if a PollID was given.

Line 7 and 8 create a connection to the access database.  If the database is in a subdirectory called db then the full path to it can be found using Server.MapPath("db\polls.mdb").


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
     ' Load the poll that matches PollID
     Set rsPoll = objConn.Execute("SELECT * FROM Polls WHERE PollID=" & PollID)
     If rsPoll.EOF Then
       Response.write "<H1>No Such Poll</H1>"
     Else ' Poll exists
         If Request.Cookies("devdomainpoll")(PollID) = "voted" Then ' Has Voted
           OptionID = false ' Don't display the vote links
         Else
           OptionID = Request.QueryString("OptionID")
           If Len(OptionID) > 0 Then ' User is trying to vote
             ' Update the count for the PollOption
             objConn.Execute "UPDATE PollOptions SET OptionSelectedCount = " & _
                     "OptionSelectedCount + 1 WHERE PollOptionID=" & OptionID
             ' Set a cookie to stop the user from voting again
             Response.Cookies("devdomainpoll")(PollID) = "voted"
             Response.Cookies("devdomainpoll").Expires = Now() + 365
             OptionID = false ' Don't display the vote links
           Else
             OptionID = true
           End If
         End If

Line 10 loads the details of the poll.

If there is no poll with the PollID given, line 12 prints a message informing the user.

Line 14 checks to see if a cookie has previously been set to indicate that the user has already voted on this poll.  If they have then the script doesn't display voting links.

Otherwise if the user has not voted before and a value has been given for OptionID line 20 increments the OptionSelectedCount for the record in the PollOptions table that has a PollOptionID that matches OptionID.

Line 23 sets the cookie that stops the user voting more then once. Line 24 sets the cookie to expire 365 days from the current date.  If Expires isn't set the cookie will be deleted when user closes their web browser.


30
31
32

33
34
35
36
37
38
39
         Response.write "<H1>" & rsPoll("PollQuestion") & "</H1>"
         ' Load the options
         Set rsPollOptions = objConn.Execute("SELECT * FROM PollOptions WHERE PollID=" _
                                        & PollID)	
         Total = 0
         Do While Not rsPollOptions.EOF
           Total = Total +  rsPollOptions("OptionSelectedCount")
           rsPollOptions.MoveNext
         Loop
         Response.write "<P>Number of votes:" & Total & "</P>"
%>

Now that we know there is a poll with the PollID given line 30 displays the Poll's question.

Line 32 loads the options for the poll from the PollOptions table.

Lines 34 to 37 loops though all the options for the poll and calculates the total of all votes.

Line 38 displays the number of people how voted on a poll.


This next bit of code displays a table with the options for the poll.

40
41
42
43
44
45
46
47
48
49

50
51

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<TABLE>
  <%
    rsPollOptions.MoveFirst
    Do While Not rsPollOptions.EOF
      If Total > 0 Then ' Calculate the percentage
        Result = Int((rsPollOptions("OptionSelectedCount") / Total) * 100)
      Else
        Result = 0
      End If
      Response.write "<TR><TD>" & rsPollOptions("PollOption") & _
                     "</TD><TD>" & Result & "%</TD>"
      If OptionID Then ' Display voting links
        Response.write "<TD><A href=""poll.asp?PollID=" & PollID & _
               "&OptionID=" & rsPollOptions("PollOptionID") & """>Vote</A></TD>"
      End If
      Response.write "</TR>"
      rsPollOptions.MoveNext
    Loop
    rsPollOptions.close
    set rsPollOptions = Nothing 
  End If
  rsPoll.close
  Set rsPoll = Nothing
  objConn.close
  set objConn = Nothing
  %>
  </TABLE>
<% ELSE %>
	<H1>No Poll Selected</H1>
<% End If %>
</BODY>
</HTML>

Lines 42 move the cursor back to the start of the record set.

Lines 43 to 55 loops though each option and displays it in a table along with the percentage of people that voted for the option, and if they have not yet voted a link to vote for an option.

Setting up the polls

To use these scripts you must first create a poll or two in the access database and options for that poll.  Place the scripts in a directory accessible from the web server and place the database in a sub directory called db.  Name the database poll.mdb.

You can download these scripts and database with sample polls below.

Download WebPoll application.

View Developers Domain's Polls

Tell a friend about this article