To see the CGI errors, please check them out here: http://www.forexballot.com/groundhog.pl
Here is my code:
my %groundhog_map = # hash that maps groundhog names to labels ( "jimmy" => "Jimmy the groundhog (Sun Prairie, Wisconsin)", "phil" => "Punxsutawney Phil (Punxsutawney, Pennsylvania)" ); print header (), start_html (-title => "Vote for Your Favorite Groundhog", -bgcolor => "white"); # Dispatch to the proper action based on user selection my $choice = lc (param ("choice")); #get choice, lowercased if ($choice eq "") #initial script invocation { display_poll_form(\%groundhog_map); } elsif ($choice eq "submit") #tally vote, show current results { process_vote (\%groundhog_map, 1, param ("name")); } elsif ($choice eq "results") #just show current results { process_vote (\%groundhog_map, 0); } else { print p (escapeHTML ("Logic error, unknown choice: $choice")); } print end_html(); sub display_poll_form { my $map_ref = shift; #groundhog name/label map print start_form (-action => url()), p ("Which groundhog is your favorite?"), #pose question radio_group (-name => "name", -values => [ sort (keys (%{$map_ref})) ], -default => "[NO DEFAULT]", -override => 1, -labels => $map_ref, -linebreak => 1), #display buttons vertically br(), submit (-name => "choice", -value => "Submit"), end_form (); #add link allowing user to see current results without voting print hr(), a ({-href => url() . "?choice=results"}, "See current resu +lts"); } CREATE TABLE groundhog ( name CHAR(10) NOT NULL, /* groundhog name */ tally INT UNSIGNED NOT NULL DEFAULT 0 /* number of votes */ ) INSERT INTO groundhog (name) VALUES ('jimmy'), ('phil') sub process_vote { my ($map_ref, $tally_vote, $name = @_; my ($dbh, $rs_ref, $row_ref, $sum, @table_row); $dbh = WebDB::connect (); if ($tally_vote) { #make sure name was given and that it's one of the legal names if (defined ($name) && defined ($map_ref->{$name})) { $dbh->do ("UPDATE groundhog SET tally = tally + 1 WHERE na +me = ?", undef, $name); print p ("Thank you for voting!"); } else { print p ("No vote was cast; did you make a choice?"); } } print p (" The current results are: "); #retrieve result set as a reference to a matrix of names and tallies $rs_ref = $dbh->selectall_arrayref ("SELECT name, tally FROM groundhog +"); $dbh->disconnect (); #compute sum of vote tallies $sum = 0; map { $sum += $_->[1] } @{$rs_ref}; if ($sum == 0) #no results! { print p ("No votes have been cast yet"); return; } #Construct table of results: header line first, then contents. #For each groundhog, show votes as a tally and as a percentage #of the total number of votes. Right-justify numeric values. push (@table_row, Tr (th ("Groundhog"), th ("Votes"), th ("Percent"))) +; foreach $row_ref (@{$rs_ref}) { my $label = $map_ref->{$row_ref->[0]}; #map name to descriptive l +abel my $percent = sprintf ("%d%%", (100 * $row_ref->[1]) / $sum); push (@table_row, Tr ( td (escapeHTML ($label)), td ({-align => "right"}, $row_ref->[1]), # tally td ({-align => "right"}, $percent) # % of total )); } print table (@table_row); }
In reply to Trouble with poll code by jdlev
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |