Guest Book Final Project - code won't save to file

Save to file does not work so subsequent functions don't work

Page 1 of 1

2 Replies - 12255 Views - Last Post: 14 May 2012 - 02:41 AM

#1 teenieBeanie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-March 09

Guest Book Final Project - code won't save to file

Post icon  Posted 16 March 2009 - 10:35 AM

Here is the situation...
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!

Is This A Good Question/Topic? 0
  • +

Replies To: Guest Book Final Project - code won't save to file

#2 KevinADC  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Guest Book Final Project - code won't save to file

Posted 16 March 2009 - 02:12 PM

THere appears to be an error here:

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

<------ MISSING CLOSING BRACKET

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);
} #en



There is no closing bracket "}" after sub validate_input

This post has been edited by KevinADC: 16 March 2009 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Guest Book Final Project - code won't save to file

Posted 14 May 2012 - 02:41 AM

View PostteenieBeanie, on 16 March 2009 - 06:35 PM, said:

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.


That's expected. If you run a script from the command line, then none of the CGI-related environment variables (such as REQUEST_METHOD) will be defined unless you manually define them.

However, the code giving you that warning is clearly not the code you posted:
$ perl -w guestbook.cgi 
Missing right curly or square bracket at guestbook.cgi line 131, at end of line
syntax error at guestbook.cgi line 131, at EOF
Execution of guestbook.cgi aborted due to compilation errors.
$
As KevinADC said, you appear to be missing a } at the end of your "validate_input" sub. (Copy/paste error?)

Fixing that, I'm able to create fake form submissions from the command line with
REQUEST_METHOD=POST perl -w guestbook.cgi
although this attempts to read the body of the POST from stdin and I can't remember how to correctly create a POST body by hand, so I cheated by replacing the body of the get_input sub with
return qw( fname lname city state country email comments );

When doing so, these dummy values are correctly written to comments.txt, I get a "thank you for the following comments" response, and running the script afterward with REQUEST_METHOD=GET instead of POST returns HTML output displaying those values.

Given that this works when run from the command line with dummy values assigned to all fields, there are two possibilities for why it doesn't work when submitting the form via CGI:

1) One or more of the field names are misspelled in the form, causing param() to not find them.

2) The web server doesn't have permission to edit comments.txt.

I took a quick look over the form you posted and everything there seems to match up with the field names the code is looking for, so my money is on #2: The user that the web server is running as (probably "nobody", "httpd", or "www-data", depending on which version of unix/linux crux.baker.edu runs) doesn't have permission to edit comments.txt. Make that file world-writable (chmod a+w comments.txt) and it should work.

If it still doesn't work, take a look in the web server's error log (if you have access to it) to see what the actual error is at runtime. If you don't have access to the error log, you may be able to get the errors displayed in your browser by adding use CGI::Carp qw(fatalsToBrowser); after use strict;.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1