Forum Moderators: coopster & phranque

Message Too Old, No Replies

Simple Form Verifier in Perl

         

web_young

9:11 pm on Aug 28, 2004 (gmt 0)

10+ Year Member



I'm trying to create a simple form verifier that will check to see if any input has been entered in each field of the form and if not it will print an error telling the user which fields they need to enter info into. It seems to be working but it's printing the errors before the header on the error output page. Does anyone know what I'm doing wrong?

#!/usr/bin/perl
#c07ex1.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 "") {
$valid = "N";
print "Please enter your name.<br>\n";
}
if ($email eq "") {
$valid = "N";
print "Please enter your email address.<br>\n";
}
if ($comments eq "") {
$valid = "N";
print "Please enter a comment.<br>\n";
}
return $valid;
} #end validate_input

sub save_to_file {
open(OUTFILE, ">>", "c07ex1.txt")
or die "Error opening c07ex1.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 the following items.</H2>\n";
print "$_[0]\n";
print "</BODY></HTML>\n";
} #end create_error_page

sub create_comments_page {
my (@records, $name_field, $email_field, $com_field);

open(INFILE, "<", "c07ex1.txt")
or die "Error opening c07ex1.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

web_young

4:27 pm on Aug 29, 2004 (gmt 0)

10+ Year Member



Nevermind, I figured it out. I needed to push the errors to an array and then print the array.

Newbie77

8:02 pm on Aug 31, 2004 (gmt 0)

10+ Year Member



How did you get this to work? I need help with this too, I keep trying to push the errors and I get stuck everytime. Thanks

web_young

12:52 am on Sep 1, 2004 (gmt 0)

10+ Year Member



Here's the script that worked for me.

#!/usr/bin/perl
#c07ex1.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, @errors);

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 "") {
$valid = "N";
push (@errors, "Please enter your name.<br>\n");
}
if ($email eq "") {
$valid = "N";
push (@errors, "Please enter your email address.<br>\n");
}
if ($comments eq "") {
$valid = "N";
push (@errors, "Please enter a comment.<br>\n");
}
return $valid;
} #end validate_input

sub save_to_file {
open(OUTFILE, ">>", "c07ex1.txt")
or die "Error opening c07ex1.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 the following items.</H2>\n";
print "@errors\n";
print "</BODY></HTML>\n";
} #end create_error_page

sub create_comments_page {
my (@records, $name_field, $email_field, $com_field);

open(INFILE, "<", "c07ex1.txt")
or die "Error opening c07ex1.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

Newbie77

9:08 pm on Sep 1, 2004 (gmt 0)

10+ Year Member



Thanks, I got this one to work finally. This one was a nightmare.