Current location: Hot Scripts Forums » Programming Languages » Perl » User-Defined Funtions (Coding the international Coffees Script)


User-Defined Funtions (Coding the international Coffees Script)

Reply
  #1 (permalink)  
Old 07-19-11, 11:48 AM
waterw1 waterw1 is offline
New Member
 
Join Date: Jul 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
User-Defined Funtions (Coding the international Coffees Script)

[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
Reply With Quote
  #2 (permalink)  
Old 07-25-11, 08:38 AM
waterw1 waterw1 is offline
New Member
 
Join Date: Jul 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
ok btw what was wrong in here was a misspelled word

sub validate_input {
my $valid="Y";
if ($name eq "" or $email eq "" or $comments eq "") {
my $vaild="N";
}
return my $vaild;

show be valid
and to take out the my will be ok for the first two red vaild
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
User Image Upload Script Phrozen44 Script Requests 12 07-16-08 12:15 AM
Submit button....can it send info to my email w/out the use of php???? lisa33 HTML/XHTML/XML 7 10-17-06 11:46 AM
Login Script v1.9 Problem SuavyDoodle JavaScript 8 09-28-06 09:13 PM
use html to open application absvinyl HTML/XHTML/XML 5 09-18-06 02:04 PM
need a script that can collect and tabulate user info DFISCELLA PHP 2 11-19-03 11:23 AM


All times are GMT -5. The time now is 04:56 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.