[SIZE="4"]This is my CGI file in perl[/SIZE]
#!/ATC-J/bin/perl.exe
#coffee.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 ($name, $email, $comments, $data_ok);
if ($ENV{'REQUEST_METHOD'} eq "POST") {
($name, $email, $comments) = get_input();
$data_ok = 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('Name'), param('Email'), param('Comments');
} #end get_input
sub validate_input {
my $valid="Y";
if ($name eq "" or $email eq "" or $comments eq "") {
my $vaild="N";
}
return my $vaild;
} #end validate_input
sub save_to_file {
open(OUTFILE, ">>", "comments.txt")
or die "Error opening comments.txt for save. $!, stopped";
print OUTFILE "$name|$email|$comments\n";
close(OUTFILE);
} #end save_to_file
sub create_acknowledgment_page {
print "<HTML>\n";
print "<HEAD><TITLE>International Coffees</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>$name, 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>International Coffees</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>Please return to the form and \n";
print "complete all items.</H2>\n";
print "</BODY></HTML>\n";
} #end create_error_page
sub create_comments_page {
my (@records, $name_field, $email_field, $com_field);
open(INFILE, "<", "comments.txt")
or die "Error opening comments.txt. $!, stopped";
print "<HTML>\n";
print "<HEAD><TITLE>International Coffees</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H2>What other coffee lovers say \n";
print "about our coffees:</H2>\n";
@records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {
chomp($rec);
($name_field, $email_field, $com_field) = split(/\|/, $rec);
print "<B>Name:</B> $name_field<BR>\n";
print "<B>Comments:</B> $com_field<BR>\n";
print "<HR>";
}
print "</BODY></HTML>\n";
} #end create_comments_page
This is my html file in perl.
<!coffee.html>
<HTML>
<HEAD><TITLE>International Coffees</TITLE></HEAD>
<BODY>
<H1>International Coffees - Comment Form</H1>
<FORM ACTION="http://localhost/cgi-bin/chap07/coffee.cgi" METHOD=POST>
<B>Your name: </B> <INPUT TYPE=text NAME=Name SIZE=30>
<B>E-mail address:</B> <INPUT TYPE=text NAME=Email SIZE=30><BR>
<P><B>Comments:</B><BR>
<TEXTAREA NAME=Comments ROWS=4 COLS=70 WRAP=Virtual></TEXTAREA></P>
<P><INPUT TYPE=submit VALUE="Submit Form">
<INPUT TYPE=reset VALUE="Reset Form"></P>
<A HREF="http://localhost/cgi-bin/chap07/coffee.cgi">
View what others have to say
</FORM></BODY></HTML>
and this is my comment.txt
Sam Spirelli|sam@ntt.com|Great coffees.
Sandra Hernandez|sandy@server.com|The Java-Mocha is the best.
When I hit submit using the browser after i have put in the information in name: Jeremiah Jacobs, email: jj@server.com, and comment: I love the flavored coffees, i always get Please return to the form and complete all items, when it should be showing.
Jeremiah Jacobs, thank you for the following comments:
I love the flavored coffees.
and it doesn't seem to be saving in the comments.txt
id ask my teacher but he isn't here!
thanks for your help