General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 16th, 2009, 01:10 PM
teenieBeanie teenieBeanie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 1 teenieBeanie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 55 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Perl Script to save input to txt file and display NOT WORKING!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek