SitePoint Sponsor

User Tag List

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

    Upgrading my guestbook using cgi

    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:

    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');
    } #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\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";
    } #end create_acknowledgment_page
     
    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";
    } #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>\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 "<a href="">Please return back to the form</a>\n";
            print "</BODY></HTML>\n";
    } #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!

  2. #2
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,272
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Assuming this is your comments:
    Code:
    foreach my $rec (@records) {
                    chomp($rec);
                    ($firstn, $lastn, $cityf, $statef, $countryf, $emailf, $commentsf) = split(/\|/, $rec);
    You'll want to get every other $rec and give it an HTML class attribute (if a $rec is a whole post from someone) and CSS sheet would style that class.

    Though, you don't seem to have a huge amount of HTML in there. I'm assuming each individual post is wrapped in a Div or some similar element. Whatever wraps the information of a post, give it a class.

    If the resulting HTML you have now really looks the way it's looking to me, it's not valid HTML and needs more block elements.


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
  •