PDA

View Full Version : voting polls


simple
03-05-2004, 07:08 PM
I'd like to put a "funniest jokes of the month" voting poll like the ones found @ the-jokes.com (http://www.the-jokes.com) on a website. I know a little HTML that's all. I've seen websites that provide these services. Any help would be appreciated - thanx

Whiteknight
03-06-2004, 01:15 PM
not a problem. first off, on your website, you will need to code out an appropriate form. consider something simple like this:


...

<form action="jokes.cgi" method="get">
<input type="radio" name="answer" value="1">Answer 1
<input type="radio" name="answer" value="2">Answer 2
<input type="radio" name="answer" value="3">Answer 3
<input type="radio" name="answer" value="4">Answer 4

<input type="submit" value="submit answer!">
</form>

...


that particular peice of code will send the poll data to a script named "jokes.cgi" but you can change that to whatever your script is named. The data will be sent through the querystring, in this form:


jokes.cgi?answer=x


where X is the value of the radio button, from 1 to 4.

next, your script (i assume you are looking for perl because this is the perl forum, but i could also walk you through the steps to do this in other languages if you prefer) should look something like this:


...

#get the value from the query string, and url-decode it
$buffer = $ENV{'QUERY_STRING'};
($name, $answer) = split(/=/, $buffer);

#read the poll data from an external data file
open(POLL1, "/path/to/poll/data");
@POLL = <POLL1>;
close(POLL);

#update the poll data
@POLL[$answer - 1] += 1;

#Open the data file again for write access (erase the file) and write
#the poll data to the file
open(POLL2, ">/same/path/to/data/file");
print <POLL2> @POLL;
close(POLL2);

...

(i threw this code together really quick, so it probably wont work without a little debugging)

now, when you click the "submit" button on your form, the data will be sent to "jokes.cgi," and jokes.cgi will keep a running tally in an unformatted textfile somewhere that you specify. if you want jokes.cgi to print out the results to a webpage afterwards, you should append a blurb like this to the end of your jokes.cgi script:


...

#this assumes the data is still in @POLL
print << "END_OF_HTML";
<html>
<body>


people who picked answer 1: $POLL[0]</p>


people who picked answer 2: $POLL[1]</p>


people who picked answer 3: $POLL[2]</p>


people who picked answer 4: $POLL[3]</p>
</body>
</html>

END_OF_HTML

...


i think that should do it for you. Good luck!

ficklejester
12-16-2004, 10:07 PM
would anyone mind completely building a poll for me with all necessary files and then having them available for download? my only experience is with html and i spent hours trying to understand and edit your examples with no luck...
thanks

Whiteknight
12-17-2004, 09:24 AM
well, it depends what kind of system you are on. the example above uses Perl/CGI, so you need to make sure that you have a Perl enabled web server. also, on a linux machine, you would need to make sure that your poll script is chmod to globally executable.

if you do all these things, and still are having a problem, you can go to a page called "Matt's Script Archive" (i cant remember the URL, do a google search), and he has a bunch of great pre-made scripts that you can download and use.