SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict jlisec01's Avatar
    Join Date
    Nov 2006
    Location
    MI
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code Perl:
    #!/usr/bin/perl
    #project3.cgi - saves form data to a file, and creates
    #a guestbook of the data sent.
    print "Content-type: text/html\n\n";
    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\n/ /;
            #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\n";
            close(OUTFILE);
    } #end save_to_file
     
    sub create_acknowledgment_page {
            print "<HTML>\n";
            print "<HEAD><TITLE>Guestbook</TITLE></HEAD>\n";
            print "<BODY>\n";
            print "<H2>Thank you for adding to our guestbook</H2>\n";
            print "<a href=http://crux.baker.edu/jlisec01/html/project/project3.html>Please return back to the form</a>\n";
            print "</BODY></HTML>\n";
    }
     
    sub create_error_page {
            print "<HTML>\n";
            print "<HEAD><TITLE>Guestbook</TITLE></HEAD>\n";
            print "<BODY>\n";
            print "<H2>Please return to the form and \n";
            print "$msg.</H2>\n";
            print "</BODY></HTML>\n";
    }
     
    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>\n";
            print "<HEAD><TITLE>Guestbook</TITLE></HEAD>\n";
            print "<BODY>\n";
            print "<H2>What other people have to say \n";
            print "on our guestbook:</H2>\n";
     
            @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>\n";
            	 print "<B>Country:</B> $countryf<BR>\n";
                     print "<B>Comments:</B> $commentsf<BR>\n";
                     print "<HR>";
            }
            print "</BODY></HTML>";
    }

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,260
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    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?

  3. #3
    SitePoint Addict jlisec01's Avatar
    Join Date
    Nov 2006
    Location
    MI
    Posts
    331
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    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?
    If I wanted to use like divs, css, and html in my cgi file do I need to do anything else special? thanks.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,260
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    If I understand you, you should be able to change the mark-up in the print lines. eg.
    Code Perl:
    sub create_comments_page {
    .....
            print "<HTML>\n";
            print "<HEAD><TITLE>Guestbook</TITLE></HEAD>\n";
            print "<BODY>\n";
            print "<H2>What other people have to say \n";
            print "on our guestbook:</H2>\n";
    .....
                 print "<B>Name:</B> $firstn  $lastn<BR>\n";
                 print "<B>Country:</B> $countryf<BR>\n";
                     print "<B>Comments:</B> $commentsf<BR>\n";
                     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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •