To read the article online, visit http://www.4GuysFromRolla.com/webtech/071099-1.2.shtml

Creating User Polls, Part 2


  • Read Part 1 of Creating User Polls

    Our poll application will take the following steps:

      1.) Check to see if the person has already voted (via a cookie)
      2.) If the user has voted, show them the results 3.) If the user has not voted, allow them to vote 4.) Once they vote, write a cookie saying they have voted and take them back to the page they came from

    We will do this with just one .ASP file. This ASP file, pollResults.asp will be called whenever a user votes on a particular item. This file will update the database count and send the user back to the page they came from (in essence it will accomplish task #4). There is code that will have to go into whatever .ASP file you want the voting to occur in. Let's take a look at this code first.

    First, this code will see if the person has already voted, by examining a cookie. We'll also go ahead and create/open our connection object and create our recordset object. Finally, we will specify the width of the poll table. This is important! This information is used to draw the bar graphs to scale:

    'Create/Open a connection object
    Dim objConn
    Set objConn = Server.CreateObject("ADODB.Connection")
    objConn.ConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & _
         Server.MapPath("Poll.mdb") & ";"
    objConn.Open

    'Create our recordset object
    Dim objRS
    Set objRS = Server.CreateObject("ADODB.Recordset")

    'Determine if this person has already voted today...
    Dim strVoted, strSQL
    strVoted = Request.Cookies("Voted")

    'Important! The width of the table
    Const iWidth = 150

    If you are not familiar with cookies, now might be a good time to read Ian Stalling's Cookie Monster article, or the Cookie Tutorial on ActiveServerPages.com.

    At this point, strVoted will contain either no value, or will contain the string "Yes." If it contains the value "Yes," then we know the user has already voted, and we want to show them the results from the poll. We'll get to that, but first let's look at what happens if they haven't voted. We want to show them the voting options. Here is our SQL to get the name of the Poll and all of the Voting Options associated with that Poll.

    strSQL = "SELECT PollOption.PollOptionID, PollOption.QuestionText, Poll.PollQuestion " & _
          "FROM Poll INNER JOIN PollOption ON Poll.PollID = PollOption.PollID " & _
          "WHERE Poll.DateStart <= #" & Date & "# AND Poll.DateEnd >= #" & Date & "#;"

    objRS.Open strSQL, objConn

    Note that in the WHERE clause we specify that we only want the poll where today's date lies between the DateStart and DateEnd parameters. Now, we want to display the Poll and its options:

    <FORM METHOD=POST ACTION="pollResults.asp" ONSUBMIT="return OneOptionChecked();" NAME=frmPoll>
        <TABLE BORDER=0 CELLSPACING=0 WIDTH=<%=iWidth%>>
        <TR><TH BGCOLOR=NAVY><FONT COLOR=WHITE FACE=Arial SIZE=2>
           <!-- Print out the title of the poll... -->
           <%=objRS("PollQuestion")%>
        </FONT></TH></TR>
        <TR><TD BGCOLOR=GRAY>
    <%
        Do While Not objRS.EOF %>
           <INPUT TYPE=RADIO NAME=radPoll VALUE="<%=objRS("PollOptionID")%>">
           <FONT FACE=Arial SIZE=2><%=objRS("QuestionText")%></FONT>
           <BR>
    <%
           objRS.MoveNext
        Loop
    %>

        <INPUT TYPE=HIDDEN NAME="URLFrom" VALUE="<%=Request.ServerVariables("SCRIPT_NAME")%>">
        <BR>
        <CENTER>
           <INPUT TYPE=SUBMIT VALUE="Vote!">
       </CENTER>
        </TD></TR></TABLE>
    </FORM>

    This will create the poll interface, as seen on the right. Looks pretty slick, eh? Feel free to change the font, colors, and other artsy fartsy stuff. What I think is super-neat about all of this is that it is dynamic, and it is a breeze to add a poll! It could even be easier with a Poll-Creation Wizard with a web interface. This wouldn't be difficult to do; in fact I challenge you to write one! :)

    Now, there are a few things not shown in the code. For example, you may have noticed in the FORM tag there is a reference to a JavaScript function OneOptionChecked(). I will not show this code in this article, but it is all in the attached files at the end of this article.

    It's time to move onto Step 3, where we'll look at submitting our votes and having them updated in the database!

  • Read Part 1 of Creating User Polls
  • Read Part 3 of Creating User Polls


  • Article Information
    Article Title: Creating User Polls, Part 2
    Article Author: Scott Mitchell
    Published Date: Saturday, July 10, 1999
    Article URL: http://www.4GuysFromRolla.com/webtech/071099-1.2.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers