Did some work to powerup the Poll demo (here it is)

Results 1 to 4 of 4

Thread: Did some work to powerup the Poll demo (here it is)

  1. #1
    Lodewijk Guest

    Default Did some work to powerup the Poll demo (here it is)

    My goal was:<BR>Take the demo and make it more flexible and portable (get pending polls, or old polls or NEW(!!) polls).<BR>First of all I created a include file which contains sub routines to make: a result table of a poll, a poll vote options table and to a routine to make a recordset depending on my sql input.<BR>Secondly I altered the JavaScript validation code (so it works for any/all forms on the same page (neat he!).<BR>The only point where I am stuck is the fact that I want to send from a page containing multiple forms (poll voting forms) my vote for that poll and that the next script recognizes for which pollid I voted (!without consulting again the database!). I tried glueing the pollid behind the radPoll radiobox value and that doesn&#039t work properly.<BR>The one who knows how to get the name of the form which send the POST hits the jackpot and the whole thing is solved.<BR><BR>P.S. If you are curious to read the code, drop me a email and I&#039ll reply you with the code.<BR><BR>Happy programming<BR>Lodewijk

  2. #2
    Lodewijk Guest

    Default Ok, forgot my email address so here is ALL the code

    #### the more reuseable validation routine ####<BR>&#060;SCRIPT LANGUAGE=JavaScript&#062;<BR> function OneOptionChecked(obj) {<BR> var i;<BR> for (i = 0; i &#060; obj.radPoll.length;i++)<BR> if (obj.radPoll[i].checked) {<BR> return true;<BR> }<BR> alert("You have to check one of the options prior to sending your vote!");<BR> return false;<BR> }<BR>&#060;/SCRIPT&#062;<BR><BR>#### my testpage called Poll.asp, the datastructure is still a bit messy, but I am planning to write a neat Poll-object to clean things up ####<BR>&#060;%@ LANGUAGE="VBSCRIPT" %&#062;<BR>&#060;% OPTION EXPLICIT %&#062;<BR>&#060;!-- #include virtual="libsource.inc" --&#062;<BR>&#060;%<BR>response.Buffer=true<BR>Co nst breedte = 150<BR>Dim Polls()<BR>Dim SQL,rst,i,j,N,M<BR><BR>&#039 we have to get the pending polls<BR>SQL = "SELECT Poll.PollID,Poll.PollQuestion,PollOption.PollOptio nID,PollOption.AnswerText,Poll.DateStart,Poll.Date End "<BR>SQL = SQL & "FROM Poll INNER JOIN PollOption ON Poll.PollID=PollOption.PollID "<BR>SQL = SQL & "WHERE Poll.DateStart &#060;= #" & Date & "# AND Poll.DateEnd &#062;= #" & Date & "#;"<BR>SQLMakeRS rst,SQL<BR>N=rst.RecordCount<BR>M=rst.Fields.Count <BR>Redim Polls(N-1,M-1)<BR>i=0<BR>Do While Not rst.EOF<BR> For j=0 to M-1<BR> Polls(i,j)=rst.Fields(j)<BR> Next<BR> i = i + 1<BR> rst.MoveNext<BR>Loop <BR>rst.Close<BR>Set rst = nothing<BR><BR>%&#062;<BR>&#060;HTML&#062;<BR>&#06 0;HEAD&#062;<BR>&#060;!-- #include virtual="checking.inc" --&#062;<BR>&#060;/HEAD&#062;<BR>&#060;BODY&#062;<BR>&#060;%<BR> Dim ID_Answer()<BR> Dim ID_Question(1)<BR> Dim Datums(1)<BR> Dim NrOfPolls,size<BR> Dim start_index()<BR> &#039 count the number of pending polls<BR> NrOfPolls=1<BR> For i=1 To N-1<BR> If Polls(i,0)&#060;&#062;Polls(i-1,0) Then<BR> NrOfPolls = NrOfPolls + 1<BR> End If<BR> Next<BR> Redim start_index(NrOfPolls-1)<BR> NrOfPolls=1<BR> start_index(0)=0<BR> For i=1 To N-1<BR> If Polls(i,0)&#060;&#062;Polls(i-1,0) Then<BR> start_index(NrOfPolls) = i<BR> NrOfPolls = NrOfPolls + 1<BR> End If<BR> Next<BR> For i=0 To UBound(start_index)<BR> &#039check the size for the ID_Answer array by <BR> If i&#060;&#062;UBound(start_index) Then<BR> size = start_index(i+1)-start_index(i)-1<BR> Else<BR> size = N-start_index(i)-1<BR> End If<BR> Redim ID_Answer(size,1)<BR> For j=0 To size<BR> ID_Answer(j,0) = Polls(start_index(i)+j,2)<BR> ID_Answer(j,1) = Polls(start_index(i)+j,3)<BR> Next<BR> ID_Question(0) = Polls(start_index(i),0)<BR> ID_Question(1) = Polls(start_index(i),1)<BR> Datums(0) = Polls(start_index(i),4)<BR> Datums(1) = Polls(start_index(i),5)<BR> response.write VoteOptions(breedte,ID_Question,ID_Answer,Datums)< BR> Next<BR>%&#062;<BR>&#060;/BODY&#062;<BR>&#060;/HTML&#062;<BR>#### now the addvote.asp script ####<BR>&#060;%@ LANGUAGE="VBSCRIPT" %&#062;<BR>&#060;% OPTION EXPLICIT %&#062;<BR>&#060;%<BR>Dim iPollOptionID,PollID,querystring,hulp,temp,i<BR>qu erystring = Request.Form<BR>&#039 let&#039s take use of the fact that the radPoll comes before the POLLID input tag !!, so when we localize the radPoll in the querystring we have the right PollID! :))<BR>hulp = split(querystring,"&")<BR>For i=0 to UBound(hulp)<BR> temp = split(hulp(i),"=")<BR> If (temp(0) = "radPoll") Then<BR> iPollOptionID = CInt(temp(1))<BR> temp = split(hulp(i+1),"=")<BR> PollID = CInt(temp(1))<BR> End If <BR>Next<BR><BR>If (iPollOptionID &#060;&#062; "" and PollId &#060;&#062; "") Then<BR> &#039go one with the thing<BR> AddVote(iPollOptionID)<BR> &#039WriteCookie(PollID) Don&#039t use this in the testing phase!<BR>Else<BR> &#039return an error because the PollOptionID and PollID were lost somewhere<BR> Response.write "An error occured, the PollOptionID and PollIDweren&#039t passed onto the addvote.asp page, Please notify the creator at webmaster@domain.com"<BR><BR>End If<BR><BR>Sub AddVote(P_O_ID)<BR> Dim sql,strProvider,con<BR> sql = "UPDATE PollOption SET Votes = Votes + 1 WHERE PollOptionID = " & P_O_ID & ";" <BR> strProvider = "Driver={Microsoft Access Driver (*.mdb)};DBQ=C:Mijn documentenPoll.mdb;"<BR> Set con = Server.CreateObject("ADODB.Connection")<BR> con.Open strProvider<BR> con.Execute sql<BR> con.Close<BR> Set con = nothing<BR>End Sub<BR>Sub WriteCookie(pollnumber)<BR> Response.Cookies("Voted")(CStr(pollnumber)) = "Yes"<BR> Response.Cookies("Voted")(CStr(pollnumber)).Expire s = DateAdd("d",7,Date)<BR> Response.Cookies("Voted")(CStr(pollnumber)).Path = "/poll"<BR>End Sub<BR><BR>response.redirect "resultpoll.asp?pollid=" & PollID<BR>%&#062;<BR>#### now the resultpoll.asp page ####<BR>&#060;%@ LANGUAGE="VBSCRIPT" %&#062;<BR>&#060;% OPTION EXPLICIT %&#062;<BR>&#060;!-- #include virtual="libsource.inc" --&#062;<BR>&#060;%<BR>Dim PollID,SQL,rst,Question<BR>Dim Array1(),Array2(1),i<BR>PollID = CInt(Request("pollid"))<BR>Const breedte = 150<BR><BR>&#039 still to be implemented here is the test if the client has allready voted for this poll, so check if a "Voted""PollID" cookie exists before continueing<BR><BR>SQL = "SELECT PollOption.AnswerText, PollOption.Votes, Poll.PollQuestion, Poll.DateStart, Poll.DateEnd FROM Poll INNER JOIN PollOption ON Poll.PollID = PollOption.PollID WHERE PollOption.PollID=" & PollID & ";"<BR><BR>SQLMakeRS rst, SQL<BR>Redim Array1(rst.RecordCount-1,1)<BR>i=0<BR>Do While Not rst.EOF<BR> If (i = 0) Then<BR> Question = rst.Fields(2)<BR> Array2(0) = rst.Fields(3)<BR> Array2(1) = rst.Fields(4)<BR> End If<BR> Array1(i,0) =rst.Fields(0)<BR> Array1(i,1) =rst.Fields(1)<BR> i = i + 1<BR> rst.MoveNext<BR>Loop <BR>rst.Close<BR>Set rst = nothing<BR>Set SQL = nothing<BR>%&#062;<BR>&#060;HTML&#062;<BR>&#060;TI TLE&#062;Results of the vote: &#060;% = Question %&#062;&#060;/TITLE&#062;<BR>&#060;BODY&#062;<BR>&#060;% response.write ResultOfVote(breedte,Question,Array1,Array2) %&#062;<BR><BR>&#060;% <BR>Set Question = nothing<BR>Set i = nothing<BR>Set PollID = nothing<BR>%&#062;<BR>&#060;/BODY&#062;<BR>&#060;/HTML&#062;<BR>

  3. #3
    Justin Bigelow Guest

    Default Question about the poll

    Hey man, I was hoping you could help me out5 since it really sounds like you know your ASP (unlike me hehe). I tried using the code provided by the author but I got an error on compilation. <BR><BR>Microsoft OLE DB Provider for ODBC Drivers error &#039 80040e10&#039 <BR><BR>[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2. <BR><BR>heres the code...<BR><BR>&#060;% else &#039this is what we show if they have not voted yet<BR> <BR> strSQL = "SELECT PollOption.PollOptionID, PollOption.QuestionText, Poll.PollQuestion " & _<BR> "FROM Poll INNER JOIN PollOption ON Poll.PollID = PollOption.PollID " & _<BR> "WHERE Poll.DateStart &#060;= #" & Date & "# AND Poll.DateEnd &#062;= #" & Date & "#;"<BR><BR> objRS.Open strSQL, objConn &#060;&#060;&#060;&#060;Thats where the error occurs<BR> %&#062;<BR><BR>Could you please shed some light on what I may be doing wrong. I even tried using the text file at the end of the artcile and saved it to asp and took out the include file but I got the same error. Thanks for any help!!!<BR><BR>Justin

  4. #4
    Lodewijk Guest

    Default RE: Question about the poll

    Hi Justin,<BR><BR>Looks like you call the objRS.Open method wrong.<BR>Have a look at what I use:<BR>Sub SQLMakeRS(rsname,sql)<BR> Dim strProvider <BR> strProvider = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=C:Mijn documentenPoll.mdb;"<BR> Set rsname = Server.CreateObject("ADODB.recordset")<BR> rsname.CursorType = 0<BR> rsname.CursorLocation = 3<BR> rsname.LockType = 1<BR> rsname.Open sql, strProvider<BR>End Sub<BR>Then you call this routine like<BR>Dim rst,sql<BR>&#039put something in sql<BR>SQLMakeRS rst, sql<BR>&#039 and the "rst" is your RecordSet made with that SQL , neat he!<BR><BR>About the fault in your code:<BR>If objConn is an created object (and ADODB.Connection object) then dump it. You don&#039t need it if you have created a ADODB.RecordSet object.<BR>So follow objRS.Open with: 1) sql code 2) provider (connection string) and then optional some connection flags.<BR>See other articles on 4guys about using connection flags.<BR><BR>P.S. My routine only creates a read (flush) connection to the database, if you need to update/edit/delete etc, set the flags correctly<BR><BR>Hopefully your problem is solved!<BR><BR>=)<BR>Lodewijk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •