CGI and Perl

Listing 4.4. Perl code that emits a form formatted using the <TABLE> object.

use CGI::Form;
 $q = new CGI::Form;
 print "<H1>Customer Survey Using Tables</H1>\n";
 print $q->start_multipart_form();
 print "<TABLE>\n<TR>\n<TD>\n";
 print "What did you order? ";
 print "</TD>\n<TD>\n";
 print $q->textfield( -name=>'order', -value=>"", -maxlength=>30, -size=>30 );
 print "</TD>\n</TR>\n<TR>\n<TD>\n";
 print "How was the service? (5-good,1-bad) ";
 print "</TD>\n<TD>\n";
 print $q->radio_group( -name=>'serviceRating', -values=>[`1','2','3','4','5'],
                       -default=>'3' );
 print "</TD>\n</TR>\n<TR>\n<TD>\n";
 print "How did the food taste? (5-good,1-bad) ";
 print "</TD>\n<TD>\n";
 print $q->radio_group( -name=>'tasteRating', -values=>[`1','2','3','4','5'],
                       -default=>'3' );
 print "</TD>\n</TR>\n<TR>\n<TD>\n";
 print "What age group are you in? ";
 @ages=( `child', `teen', `20s', `30s', `40s', `50s', `senior' );
 print "</TD>\n<TD>\n";
 print $q->popup_menu( -name=>'ageGroup', -values=>\@ages );
 print "</TD>\n</TR>\n<TR>\n<TD>\n";
 print $q->checkbox( -name=>'comeAgain', -checked=>'checked', -value=>'yes',
                     -label=>"I would come back again " );
 print "</TD>\n</TR>\n<TR>\n<TD>\n";
 print $q->submit( -name=>'Action', -value=>'Submit' );
 print $q->reset();
 print "</TABLE>";
 print $q->endform();

The form generated by this script will appear in the browser as shown in Figure 4.2.

Figure 4.2. The questionnaire, using a table, as it appears in the browser.

Using Netscape Frames You can also use Netscape frames to display multiple pages within a single browser window. Frames are defined using the <FRAME> tag, where each frame is associated with a single URL. You can have multiple CGI scripts running to produce a single Web page where each script runs within its own frame. An example HTML document that incorporates two frames running each of the above CGI programs follows:

 <FRAMESET ROWS=50%,50%>
    <FRAME SRC="http://vaders/cgi-bin/wpp/testform.cgi">
    <FRAME SRC="http://vaders/cgi-bin/wpp/tablform.cgi">


The <FRAME> tag is not an HTML 2.0 or 3.2 tag. It is a Netscape extension that is also supported in Microsoft's Internet Explorer 3.0. If you are concerned about compatibility with other browsers, you should avoid using frames or provide a non-frame alternative.

In this example, the page displays two horizontal frames of equal size. In the top frame, it will execute, and in the bottom frame, it will execute The frames are divided evenly across the page. The resulting page of this example is shown in Figure 4.3.

Figure 4.3. Both forms displayed in separate Netscape frames.