Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need of Serious help: CGI scripts for polls and nomination forms

Status
Not open for further replies.

minhtam2448

Technical User
Jan 4, 2008
8
0
0
US
Hi. I just picked up an interest in PERL and CGI programming and I want to run a little popularity contest within anime characters. Unfortunately, I am unsure of my knowledge within CGI to do so. I have a lot of questions to ask, so please bear with me.

At this point, I am unsure of what to ask, since I believe my questions are more progressive, but I am trying to understand and figure out how to write a CGI program to accept GET and post INPUTS, and compile it in a way so that it adds 1 to the total, in other words, tally votes.

A friend of mine provided me a script that may prove to be very useful. The # comments are written by me to show how much of the script I understand. (The ones colored in red are not part of the script, as comments shouldn't be written between the curly brackets.

sub GetFormInput # calls the subroutine

{
(*fval) = @_ if @_ ; # ???

local ($buf); # defines variables
if ($ENV{'REQUEST_METHOD'} eq 'POST')
{
read(STDIN,$buf,$ENV{'CONTENT_LENGTH'}); # reads input, stores it in variable (?), takes the Content Length environmental variable
}
else
{
$buf=$ENV{'QUERY_STRING'}; # variable equals the query string
}

if ($buf eq "")
{
return 0 ; # if buf variable is empty, return a zero value or false
}
else # From here to the end is where I get lost
{
@fval=split(/&/,$buf);
foreach $i (0 .. $#fval)
{
($name,$val)=split (/=/,$fval[$i],2);
$val=~tr/+/ /;
$val=~ s/%(..)/pack("c",hex($1))/ge;
$name=~tr/+/ /;
$name=~ s/%(..)/pack("c",hex($1))/ge;

if (!defined($FORM{$name}))
{
$FORM{$name}=$val;
}
else
{
$FORM{$name} .= ",$val";

#if you want multi-selects to goto into an array change to:
#$field{$name} .= "\0$val";
}
}
}
return 1;
}

So I guess my first few questions are thus:

1. Am I understanding this script correctly thus far?
2. Can anybody explain everything from where I don't understand the script to the "return 1" command?
 
Thanks for the quick response. I've looked it over, and it definitely looks easier, but I'm still unsure about whether I really understand and know what I'm doing.

For now, I've written a sample script as a test, but I'm not sure if I've even hit first base. Here's the HTML form template that I've been using: (by the way, below isn't really my e-mail...


<head>

<title>International Saimoe League</title>
<link rev="made" href="mailto:ka_rei_tsukai_hiita@yahoo.com">

</head>

<body>

<hr>
<h1>Arena 0</h1>

<p>

Welcome to the arena. Please place your vote for your favorite contestant.

<p>

<form action=" method="post">

<input type="radio" name="moe" value="abstain"> Abstain<br>
<input type="radio" name="moe" value="x"> Contestant A<br>
<input type="radio" name="moe" value="y"> Contestant B<br>
<input type="submit" value="vote">
</form>

<p></body>


to which the form action should revert to the following script.


#!/usr/local/bin/perl

use CGI qw:)standard);

my $error = cgi_error;
if ($error) {
print header(-status=>$error),
start_html('Technical Error.'),
h2('Technical Difficulties'),
'You have experienced an error and your vote has not been processed. Please report the error to the administrator.',
strong($error);
exit 0;
}

else{

print header
print start_html(-title=>'International Moe League',
-header=>'TEST POLL');

$vote = param('moe');

if ($vote eq "abstain")
{print 'You have abstained/canceled your vote.'}
elsif ($vote eq 'x|y')
{print 'Thank you for voting in this match.'}
else {&veto("Not a valid vote"); }

print 'Please click on the link below to return to the International Moe League website',
a({-href=>"

print end_html();

sub veto {
print "<P>$_[0]</P";
print end_html;
die; }


From here, I don't know whether I'm missing anything I need or if I have any syntax errors. All I know is that so far, it doesn't work. Is there anything I can do to make the script compatible with the form template and make it more efficient?

And another question I had in mind is: Is it possible to send a user's input to an e-mail address along with the remote address?
 
You have a missing closing bracket '}'. The "else" condition is not terminated, put one after this line:

Code:
print end_html();
}  #<-- ending bracket

If everything else is good that should get the program to compile.

This is also wrong:

Code:
 elsif ($vote eq 'x|y')

should be:

Code:
 elsif ($vote eq 'x' || $vote eq 'y')


Programming is very much a details oriented affair, sometimes tediously so. Make sure all blocks of code begin and end properly. Using proper indentation helps spot errors. Your code is rather sloppily formmated, at least it is in your post. Also note that your html code is improperly written:

Code:
print "<P>$_[0]</P";

the ending P tag has no '>' on the end.

Start using "strict" and "warnings" now instead of later. Use the CGI::Carp module to help debug CGI scripts:

Code:
use strict;
use warnings;
use CGI::Carp qw/fatalsToBrowser/;
#your code follows

Your script is actually pretty good for a total perl newbie, so I think you will catch on pretty fast after the initial learning phase is over. Perl based CGI scripts are a little difficult at first, there is a lot to learn, but keep asking questions and reading any resource material you have or are directed to read and in a couple of days you will have a functioning script. Then you can start working on getting it secure (data input validation is important) and adding more features.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top