I have a guestbook form in html that passes the values to my variables. When I click submit, it gives me a blank webpage and none of the entered information gets put into the comments.txt file. I don't get the acknowledgment page or the ability to view previous comments.
here is my form code:
<head> <title>Guest Book</title> </head> <body> <h1 align="center">Guest Book</h1> <hr> </p> <FORM NAME="orderForm" ACTION="https://crux.baker.edu/~cvergi01/cgi-bin/chap07/guestbook.cgi" METHOD=POST > <table width="200" border="0" align="center"> <tr> <td>First Name:</td> <td>Last Name:</td> </tr> <tr> <td> <input type="text" name="fname" id="fname"> </td> <td> <input type="text" name="lname" id="lname"> </td> </tr> <tr> <td>City:</td> <td>State:</td> </tr> <tr> <td> <input type="text" name="city" id="city"> </td> <td> <input type="text" name="state" id="state"> </td> </tr> <tr> <td>Country:</td> <td>E-Mail Address:</td> </tr> <tr> <td> <input type="text" name="country" id="country"> </td> <td> <input type="text" name="email" id="email"> </td> </tr> <tr> <td colspan="2"><div align="center">Comments</div></td> </tr> <tr> <td colspan="2"> <textarea name="comments" id="comments" cols="45" rows="5"></textarea> </td> </tr> </table> <p align="center"> <input type="submit" value="Add Your Name" /> <input type="reset" value="Clear Form" /> <br><br> <A HREF="http://crux.baker.edu/cgi-bin/chap07/guestbook.cgi"> View what others have to say. </A> </p> </form> </body> </html>
and my cgi script:
#!/usr/bin/perl
#guestbook.cgi - saves form data to a file, and creates
#three different dynamic Web pages
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);
#prevent Perl from creating undeclared variables
use strict;
#declare variables
my ($fname, $lname, $city, $state, $country, $email, $comments, $data_ok, $msg );
if ($ENV {'REQUEST_METHOD'} eq "POST") {
($fname, $lname, $city, $state, $country, $email, $comments) = get_input();
($fname, $lname, $city, $state, $country, $email, $comments) = format_input();
($data_ok, $msg) = validate_input();
if ($data_ok eq "Y") {
save_to_file();
create_acknowledgment_page();
}
else {
create_error_page();
}
}
else {
create_comments_page();
}
exit;
#*****user-defined functions*****
sub get_input {
return param('fname'), param('lname'), param('city'), param('state'), param('country'), param('email'), param('comments');
} #end get_input
sub format_input {
#declare and assign values to temporary variables
my ($fn, $ln, $ci, $st, $cou, $com, $e);
($fn, $ln, $ci, $st, $cou, $com, $e) = ($fname, $lname, $city, $state, $country, $comments, $email);
#remove leading and trailing spaces from fname
$fn =~ s/^ +//;
$fn =~ s/ +$//;
#remove leading and trailing spaces from lname
$ln =~ s/^ +//;
$ln =~ s/ +$//;
#remove leading and trailing spaces from city
$ci =~ s/^ +//;
$ci =~ s/ +$//;
#remove leading and trailing spaces from state
$st =~ s/^ +//;
$st =~ s/ +$//;
#remove leading and trailing spaces from country
$cou =~ s/^ +//;
$cou =~ s/ +$//;
#remove leading and trailing spaces from email
$e =~ s/^ +//;
$e =~ s/ +$//;
#remove leading and trailing whitespace character from comments
$com =~ s/^\s+//;
$com =~ s/\s+$//;
#replace return and new line combination within comments with a space
$com =~ tr/\r\n/ /;
#remove extra spaces from within comments
$com =~ tr/ //s;
return $fn, $ln, $ci, $st, $cou, $com, $e;
} #end format input
sub validate_input {
my $valid ="Y";
my $errormsg;
if ($fname eq "" or $lname eq "" or $city eq "" or $state eq "" or $country eq "" or $email eq "" or $comments eq "") {
$valid = "N";
$errormsg = "complete all items";
}
return $valid, $errormsg;
#end validate_input
sub save_to_file {
open(OUTFILE, ">>", "comments.txt")
or die "Error opening comments.txt for save. $!, stopped";
print OUTFILE "$fname|$lname|$city|$state|$country|$email|$comments\n";
close(OUTFILE);
} #end save_to_file
sub create_acknowledgment_page {
print "<HTML>\n";
print "<HEAD><TITLE> Guest Book</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>$fname $lname, from $city, $state, thank you for the following \n";
print "comments:<BR><BR>$comments\n";
print "</H2></BODY></HTML>\n";
} #end create_acknowledgment_page
sub create_error_page {
print "<HTML>\n";
print "<HEAD><TITLE>Guest Book</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>Please return to the form and \n";
print "$msg.</H2>\n";
print "</BODY></HTML>\n";
} #end create_error_page
sub create_comments_page {
my ($fname_field, $lname_field, $city_field, $state_field, $country_field, $email_field, $comments_field);
open(INFILE, "<", "comments.txt")
or die "Error opening comments.txt. $!, stopped";
print "<HTML>\n";
print "<HEAD><TITLE>Guest Book</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>What other visitors have to say say \n";
print "about our site:</H2>\n";
while (<INFILE>) {
chomp($_);
($fname_field, $lname_field, $city_field, $state_field, $country_field, $email_field, $comments_field) = split(/\|/, $_);
print "<B>Name:</B> $fname_field $lname_field<BR>\n";
print "<B>City:</B> $city_field<BR>\n";
print "<B>State:</B>$state_field<BR>\n";
print "<B>Country:</B>$country_field<BR>\n";
print "<B>Email:</B>$email_field<BR>\n";
print "<B>Comments:</B> $comments_field<BR>\n";
print "<HR>";
}
close (INFILE);
print "</BODY></HTML>\n";
} #end create_comments_page
When I do the perl -w guestbook.cgi, it says "Use of uninitialized value in string eq at guestbook.cgi line 13." which is where my $ENV{'REQUEST_METHOD} is... but I don't understand why that would be wrong.
Please let me know if you see what might the problem. My teammate and I have been working on this for days and we can't figure out what's wrong.
Thanks!

New Topic/Question
Reply



MultiQuote





|