|
Perl Script to save input to txt file and display NOT WORKING!
Hi everyone,
I have can assignment due tonight that involves creating a guestbook. I have already created the HTML form and I can't seem to get my Perl to function properly. It runs through it, I don't get errors. But when it should create the acknowledgment page and the comments page, it doesn't. It just gives me a blank page. Also, it is not storing the data in the comments.txt file at all.
Can anyone help, please?
Here is my code:
Code:
#!/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|$commen ts\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
This is my final project and my partner and I have been working on it for days. I can't figure out what I'm missing.
Please help. Thank you.
|