Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Skill 7
Creating a Guest Book for Your Web Site

  Designing the guest book
  Adding guest book entries
  Displaying the guest book
  What’s in a form: security issues

One of the wonderful capabilities of the World Wide Web is that it can be used as a repository. Information stored anywhere can be retrieved—from anywhere—so long as it’s accessible to and from the Web. Research avenues have been blown wide open by the Web.

So far, the Perl-CGI programs you’ve written have provided instant feedback for your visitors. But once the information has been entered and displayed, it’s gone; it can’t be retrieved again, because it was never stored anywhere.

In this skill you’ll design a “guest book” Web page that will allow visitors to enter pertinent details about themselves. You’ll write CGI programs in Perl to get a guest book entry for a single visitor, store the entry in a disk file, and display the entire roster of visitors. You’ll also learn the basics of Web site security and what you can do in your CGI programs to prevent visitors from inadvertently or maliciously entering data that could damage your site or your system.

Designing the Guest Book

A good guest book design is simple, but not too much so. You want a visitor to provide enough information to benefit you and anyone who calls up the list. Conversely, most visitors won’t take the time to write their autobiographies, so don’t expect them to. You’ll take several steps to ensure that they don’t, in fact, because you don’t want visitors unnecessarily eating up disk space on your Web site. Figure 7.1 illustrates a Web site with a guest book. If it looks familiar, it’s because we used it as an example in Skill 5.


Figure 7.1:  An example of a guest book Web page

Let’s take a look at the HTML source for a page similar to the one illustrated in Figure 7.1. You can call it guestbook.html.

   <HTML>
   <HEAD>
   <TITLE>Perl-CGI Guest Book</TITLE>
   </HEAD>

   <BODY>
   <CENTER>
   <H1 ALIGN="CENTER">Perl-CGI Guest Book</H1>
   <HR>

   <FORM ACTION="/scripts/perl-cgi/addguest.pl"
   METHOD="POST">

   <TABLE WIDTH="50%">

   <TR>
   <TD><STRONG>First name: </STRONG></TD>
    <TD><STRONG>Last name: </STRONG></TD>
   </TR>
   <TR>
   <TD> <INPUT TYPE="text"
   NAME="first_name" SIZE="24" MAXLENGTH="30"> </TD>
   <TD> <INPUT TYPE="text" NAME="last_name";
    SIZE="24" MAXLENGTH="30"> </TD>
   </TR>
   </TABLE>

   <TABLE WIDTH="50%">

   <TR>
   <TD><STRONG>City: </STRONG></TD>
    <TD><STRONG>State: </STRONG></TD>
   </TR>
   <TR>
   <TD> <INPUT TYPE="text" NAME="city" SIZE="24"
    MAXLENGTH="30"> </TD>
   <TD> <INPUT TYPE="text" NAME="state" SIZE="24"
    MAXLENGTH="30"> </TD>
   </TR>
   </TABLE>
   <TABLE WIDTH="50%">

   <TR>
   <TD><STRONG>Country: </STRONG></TD>
    <TD><STRONG>E-mail address: </STRONG></TD>
   </TR>
   <TR>
   <TD> <INPUT TYPE="text" NAME="country" SIZE="24"
    MAXLENGTH="30"> </TD>
   <TD> <INPUT TYPE="text" NAME="email" SIZE="24"
    MAXLENGTH="72"> </TD>
   </TR>
   </TABLE>

   <STRONG>Comments</STRONG><BR>
   <TEXTAREA NAME="comments" COLS="36" ROWS="6" WRAP="virtual"></TEXTAREA>

   <BR>
   <P>
   <INPUT TYPE="submit" VALUE="Add your name">
   <INPUT TYPE="reset" VALUE="Clear form">
   </P>
   </FORM>
   </CENTER>

   </BODY>
   </HTML>

Called up in your Web browser, the guest book form should look similar to what is shown in Figure 7.2


Figure 7.2:  The entry page for your guest book

This page is a pretty good example of how tables are constructed in HTML. Notice the table declaration at the top:

   <TABLE WIDTH="50%">

   <TR>
   <TD><STRONG>First name: </STRONG></TD> <TD><STRONG>Last name:
    </STRONG></TD>
   </TR>
   <TR>
   <TD> <INPUT TYPE="text" NAME="first_name" SIZE="24"
    MAXLENGTH="30"> </TD>
   <TD> <INPUT TYPE="text" NAME="last_name" SIZE="24"
    MAXLENGTH="30"> </TD>
   </TR>
   </TABLE>

This code sets the width of the table to 50 percent of the screen width and then formats the rows and columns for the first block of text fields, in this case for the visitor’s first and last names.


TIP:  It’s a good idea to use percentages rather than fixed values for setting the width of a table. Your visitors will have a variety of screens, each running at different resolutions. An HTML table that has its width set to a certain number of pixels will be larger or smaller, depending on the display resolution—in extreme cases, it might not even fit on the screen. Using the percentage width ensures that your tables will be sized to fit the screen proportionally, regardless of the visitor’s equipment.

The text fields have been given MAXLENGTH values, too, which helps to prevent a user from entering too much data in a field.

Notice, too, that each block of the form is set up in its own table. It makes the page neat and tidy, and it’s easier to make additions or subtractions later on when the tables are laid out this way.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.