PDA

View Full Version : need help with cgi poll


artificialg
07-20-2004, 04:39 AM
ok i have a cgi poll up at my website. you can check it out at http://www.artificialg.net the poll works fine, but i want the results to be displayed on the homepage instead of a separate page being loaded with the reuslts of the cgi poll. essentially, i want my homepage to reload once someone has voted and display the results of the poll where the poll question is before someone votes. does anyone know how i can accomplish this? javascript possibly? i've done some extensive searching on the internet, but couldn't find anything. thanks in advance.

edit: here is the cgi code in case it helps.



#!/usr/local/bin/perl

if ($ENV{'REQUEST_METHOD'} eq 'POST') {

# Get the input

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs

@pairs = split(/&/, $buffer);

# Load the FORM variables

foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$FORM{$name} = $value;
}
}

print "Content-type: text/html\n\n";

if(-e "poll.dat")
{
open(POLL, "poll.dat");
}

while(<POLL>)
{
push(@poll, $_);
}
close(POLL);

@lines = ("1");
foreach $each_line (@lines)
{
$this_line = "line" . $each_line;
$$this_line = $poll[$each_line-1];
chop($$this_line);
}

if($line1 eq " ")
{
$line1 = "0\t0\t0";
}

@line1_answers = split(/\t/, $line1);

if($FORM{'QUESTION'} eq "yes")
{
$line1_answers[0]++;
}

elsif($FORM{'QUESTION'} eq "no")
{
$line1_answers[1]++;
}



open(POLL, ">poll.dat");
print POLL "$line1_answers[0]\t$line1_answers[1]\n";
close(POLL);

print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE>artificialg.net</TITLE>\n";
print "</HEAD>\n";
print "<BODY BGCOLOR=white>\n";
print "<font face=arial color=#0000bb size=1>\n";
print "do you recycle?<BR><BR>\n";

print "<font face=arial color=#0000bb size=1>\n";

print "yes ";


print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";

print $line1_answers[0] / ($line1_answers[0] + $line1_answers[1])*100;

print "&nbsp;%";

print "<BR><BR>\n";

print "no ";

print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";

print $line1_answers[1] / ($line1_answers[0] + $line1_answers[1])*100;

print "&nbsp;%";
print "</TD>";


print "</BODY>\n";
print "</HTML>\n";

MattJakel
07-20-2004, 05:54 AM
The only way I know to accomplish this is really complicated (as is everything I do in Perl since I never do it the proper way lol). What you can do though is add comment tags around the poll like this: <!--poll-->...<!--/poll-->, and have the script open the html file and replace <!--poll-->...<!--/poll--> with the results. This is a really complicated way to do it, but it's the only way I know.

Hope this helps!
Matt :thumbsup:

mlseim
07-20-2004, 02:43 PM
Does your host allow SSI (Server Side Includes)?
That would be the easiest.

Or maybe do it like a site counter ...
The numbers are actually .gif images that are loaded
and displayed. Do a Google search for free Perl site
counter scripts and use the "display" part for your poll.

dswimboy
07-20-2004, 10:39 PM
i would say use SSI.

have a script check for a cookie on the user's system, to see if they've voted.

if they have, display the results. if not, display the poll.

another script would process the voting form. first set the cookie, if it fails, then don't update the results (cuz then someone could vote forever). have the script redirect to the homepage.