CGI and Perl

Displaying the Form

The form you use to obtain this information will use many of the field types described in Chapter 4, "HTML Forms--The Foundation of an Interactive Web." You'll also use a table to make the Figure 7.1. The guest book form as it appears in the browser. fields align nicely. Ultimately, you should end up with a form as shown in Figure 7.1.


Figure 7.1.

To display this form, you can use a combination of <TABLE> tags and CGI::Form methods, as in the Perl subroutine in Listing 7.1.

Listing 7.1. Perl subroutine for printing the guest book form.

sub guestBookForm {
    my($q)=@_;
    my(@states)=(`AK','AL','AR','AZ','CA','CO','CT','DE','FL','GA',
                 `HI','IA','ID','IL','IN','KS','KY','LA','MA','MD',
                 `ME','MI','MN','MO','MS','MT','NC','ND','NE','NH',
                 `NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC',
                 `SD','TN','TX','UT','VA','VT','WA','WI','WV','WY');
    my(@hows)=(`A Friend','Magazine','Newspaper','Television',
                 `Sales Person','Other');
    my(@products)=(`Widget','Whatsit','Whatchamacallit','Thingamajig');
    print "<H1>Welcome to Widget World</H1>\n";
    print "<P>Please take a moment to fill out our guest book ";
    print "to help us serve you better.\n";
    print $q->start_multipart_form();
    print "<TABLE>\n";
    print "<TR>\n";
    print "<TD>Name:<TD>\n";
    print $q->textfield(-name=>`Name',-size=>32,-maxlength=>32);
    print "<TR><TD>Title:<TD>\n";
    print $q->textfield(-name=>`Title',-size=>32,-maxlength=>32);
    print "<TR><TD>Company:<TD>\n";
    print $q->textfield(-name=>`Company',-size=>32,-maxlength=>32);
    print "<TR><TD>Address:<TD>\n";
    print $q->textarea(-name=>`Address',-rows=>3,-cols=>32);
    print "<TR><TD>City:<TD>\n";
    print $q->textfield(-name=>`City',-size=>32,-maxlength=>32);
    print "<TR><TD>State:<TD>\n";
    print $q->popup_menu(-name=>`State',-value=>\@states);
    print "<TR><TD>Zip Code:<TD>\n";
    print $q->textfield(-name=>`ZipCode',-size=>10,-maxlength=>10);
    print "<TR><TD>Phone:<TD>\n";
    print $q->textfield(-name=>`Phone',-size=>12,-maxlength=>12);
    print "<TR><TD>Fax:<TD>\n";
    print $q->textfield(-name=>`Fax',-size=>12,-maxlength=>12);
    print "<TR><TD>e-mail:<TD>\n";
    print $q->textfield(-name=>`email',-size=>32,-maxlength=>32);
    print "<TR><TD>How did you hear about us?<BR>";
    print $q->radio_group(-name=>`How',-values=>\@hows,-linebreak=>`true');
    print "<TD>Which product(s) are you interested in?<BR>";
    print $q->scrolling_list(-name=>`What',-values=>\@products,
                             -multiple=>`true'-linebreak=>`true');
    print "</TABLE>\n";
    print "Any Comments?<BR>\n";
    print $q->textarea(-name=>`Comment',-rows=>10,-cols=>60);
    print "<HR>\n";
    print $q->submit(-name=>`Action',-value=>`Submit');
    print " ";
    print $q->reset(-value=>`Start from Scratch');
    print $q->endform();
 }