|
HELP:perl CGI problem
Here is the HTML source and the corresponding CGI program ,but the CGI program cannot be correctly executed as expected.
If any advice could be given,it would be great!
Thx a lot.
HTML:
<HTML>
<TITLE>vote program</TITLE>
<h1>vote program</h1>
<FORM METHOD="post" ACTION="/cgi-bin/vote.pl">
who do you like better?<br>
<input type="radio" name="idol" value="zhang" checked>A<br>
<input type="radio" name="idol" value="ajiax">B<br>
<input type="radio" name="idol" value="sangbulas">C<br>
<input type="radio" name="idol" value="buck">D<br>
<input type="radio" name="idol" value="guyi">E<br>
<input type="submit" value="execute">
<input type="reset" value="abort">
</form>
query<a href="/cgi-bin/vote.pl?command=view">vote result</a>
</html>
CGI:
#! /usr/bin/perl -wT
print"Content-Type: text/html\n\n";
print"<title>vote</title>";
if($ENV{'REQUEST_METHOD'}eq"POST"){
read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
}elsif($ENV{'REQUEST_METHOD'}eq"GET"){
$buffer=$ENV{'QUERY_STIRNG'};
}
@pairs=split(/&/,$buffer);
foreach $pair(@pairs){
($name,$value)=split(/=/,$pair);
$value=~tr/+//;
$value=~s/%([a-f A-F 0-9][a-f A-f 0-9])/pack("C",hex($1))/eg;
$FORM{$name}=$value;}
$filename="/vote.dat";
%NAME=("A","zhang","B","ajiax","C","sangbulas","D","buck","E","guyi");
if($ENV{'REQUEST_METHOD'}eq"POST"){
print"Content-type:text/html\n\n";
print"<title>vote system</title>";
print"<h1>an example of the vote system</h1>";
open(FILE,"<$filename")||die"cannot open the file.please contact with the system manager\n";
for($i=0;$i<2;$i++){
$file[$i]=<FILE>;
$file[$i]=~s/\n$//;
}
close(FILE);
@item=split(/:/,$file[0]);
@vote=split(/:/,$file[1]);
for($i=0;$i<@item;$i++){
if($FORM{'idol'}eq$item[$i]){
$vote[$i]++;
last;
}
}
open(FILE,">filename")||die"Can't Open the file";
$item=join(":",@item);
$vote=join(":",@vote);
pirnt FILE "$item\n";
print FILE "$vote\n";
close (FILE);
print"<h2>you vote for$NAME{$FORM{'idol'}},thans for voting!<h2>";
print"query<a href=\"/cgi-bin/vote.pl?command=viem\">result</a>";
}
if($FORM{'command'}eq"view"){
print "Content-type:text/html\n\n";
print"<title>result</title>";
print"<h1>result</h1>";
open (FILE,"$filename")||die"cannot open the file";
for($i=0;$i<2;$i++){
$file[$i]=<FILE>;
$file[$i]=~s/\n$//;
}
close(FILE);
@item=split(/:/,$file[0]);
@vote=split(/:/,$file[1]);
print"<table border=1>";
for($i=0;$i<@item;$i++){
print"<tr><td>name</td><td>$NAME{$item[$i]}</td><td>count</td>,td>$vote[$i]</td><tr>";
}
print "</table>";
}
|