Displaying html in a cgi file

Hi there, I have yet another problem that I’m trying to figure out. I have a cgi script that is a guestbook, the code is below. However, I have a website that I want to be able to display the guestbook comment posts within a table or something where I have control over. I was able to use the html template in the cgi file but I had no control over where to place my guestbook posts, whenever I submitted posts from the form, the results would be all scattered over. If anyone could help I’d really appreciate it, here is my original code for the guestbook:

I pretty much like to know where would I place my html code for the site, I appreciate all the help, thank you.

#!/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');
}
        
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>\
";
}

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>\
";
}
        
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 "</BODY></HTML>";
}

If I understand you, you should be able to change the mark-up in the print lines. eg.

sub create_comments_page {
.....
        print "<HTML>\
";
        print "<HEAD><TITLE>Guestbook</TITLE></HEAD>\
";
        print "<BODY>\
";
        print "<H2>What other people have to say \
";
        print "on our guestbook:</H2>\
";
.....
             print "<B>Name:</B> $firstn  $lastn<BR>\
";
             print "<B>Country:</B> $countryf<BR>\
";
                 print "<B>Comments:</B> $commentsf<BR>\
";
                 print "<HR>";
        }
        print "</BODY></HTML>";

just use what ever tags you want. I guess you could use inline style but I don’t see why you couldn’t put a style tag in the head instead.

If I wanted to use like divs, css, and html in my cgi file do I need to do anything else special? thanks.

I had to change the bash line, comment out the 2 use lines, and manually create a comments.txt file to test it. What do you mean by “all scattered over”? Not in the same sequence as in the file?