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
Skill 7 Creating a Guest Book for Your Web Site
- Designing the guest book
- Adding guest book entries
- Displaying the guest book
- Whats 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 retrievedfrom anywhereso long as its accessible to and from the Web. Research avenues have been blown wide open by the Web.
So far, the Perl-CGI programs youve written have provided instant feedback for your visitors. But once the information has been entered and displayed, its gone; it cant be retrieved again, because it was never stored anywhere.
In this skill youll design a guest book Web page that will allow visitors to enter pertinent details about themselves. Youll 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. Youll 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 wont take the time to write their autobiographies, so dont expect them to. Youll take several steps to ensure that they dont, in fact, because you dont 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, its because we used it as an example in Skill 5.
Figure 7.1: An example of a guest book Web page
Lets 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 visitors first and last names.
TIP: Its 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 resolutionin 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 visitors 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 its easier to make additions or subtractions later on when the tables are laid out this way.
|