Hi there, I got a guest book that I did in cgi and I was wondering if I could do something like what they refer to as a zebra stripe, the first post has a background color of grey and then the next post has a background color of light green or something, and so on. Here is my guestbook code currently:
#!/usr/bin/perl
#project3.cgi - saves form data to a file, and creates
#a guestbook of the data sent.
print "Content-type: text/html\
\
";
use CGI qw(:standard);
#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, $country, $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('firstname'), param('lastname'), param('city'), param('state'), param('country'), param('email'),
param('Comments');
} #end get_input
sub format_input {
#declare and assign values to temporary variables
my ($f, $l, $co, $cm);
($f, $l, $co, $cm) = ($fname, $lname, $country, $comments);
#remove leading and trailing spaces from first name
$f =~ s/^ +//;
$f =~ s/ +$//;
#remove leading and trailing spaces from last name
$l =~ s/^ +//;
$l =~ s/ +$//;
#remove leading and trailing spaces from country
$co =~ s/^ +//;
$co =~ s/ +$//;
#remove leading and trailing whitespace characters
#from comments
$cm =~ s/^\\s+//;
$cm =~ s/\\s+$//;
#replace return and newline combination within comments
#with a space
$cm =~ tr/\\r\
/ /;
#remove extra spaces from within comments
$cm =~ tr/ //s;
return $f, $l, $co, $cm;
} #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";
}
elsif ($email !~ m/[\\w\\-]+\\@[\\w\\-]+\\.[\\w\\-]+/) {
$valid = "N";
$errormsg = "enter a valid e-mail address";
}
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|$state|$city|$country|$email|$comments\
";
close(OUTFILE);
} #end save_to_file
sub create_acknowledgment_page {
print "<HTML>\
";
print "<HEAD><TITLE>Guestbook</TITLE></HEAD>\
";
print "<BODY>\
";
print "<H2>Thank you for adding to our guestbook</H2>\
";
print "<a href=http://crux.baker.edu/jlisec01/html/project/project3.html>Please return back to the form</a>\
";
print "</BODY></HTML>\
";
} #end create_acknowledgment_page
sub create_error_page {
print "<HTML>\
";
print "<HEAD><TITLE>Guestbook</TITLE></HEAD>\
";
print "<BODY>\
";
print "<H2>Please return to the form and \
";
print "$msg.</H2>\
";
print "</BODY></HTML>\
";
} #end create_error_page
sub create_comments_page {
my (@records, $firstn, $lastn, $cityf, $statef, $countryf, $emailf, $commentsf);
open(INFILE, "<", "comments.txt")
or die "Error opening comments.txt. $!, stopped";
print "<HTML>\
";
print "<HEAD><TITLE>Guestbook</TITLE></HEAD>\
";
print "<BODY>\
";
print "<H2>What other people have to say \
";
print "on our guestbook:</H2>\
";
@records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {
chomp($rec);
($firstn, $lastn, $cityf, $statef, $countryf, $emailf, $commentsf) = split(/\\|/, $rec);
print "<B>Name:</B> $firstn $lastn<BR>\
";
print "<B>Country:</B> $countryf<BR>\
";
print "<B>Comments:</B> $commentsf<BR>\
";
print "<HR>";
}
print "<a href="">Please return back to the form</a>\
";
print "</BODY></HTML>\
";
} #end create_comments_page
Idk if anyone can help me out, maybe something to at least get me start, I’d really appreciate it, thanks!