7.8 Form Widget Methods
The here document, though handy, may not be the best way to build every HTML page. CGI.pm provides several other methods to create widgets, including:
start_form()
start tag for the form
textfield()
tag for a text widget
textarea()
tag for a text area
radio_group()
tags for a radio button group
checkbox()
tag for a check box
popup_menu()
tag for a pop-up menu
scrolling_list()
tags for a scrolling list box
submit()
tag for a submit button
reset()
tag for a reset button
end_form()
</FORM>
The following example, /var/www/cgi-bin/widgets3.cgi, uses these methods and produces the same form shown in Figure 7.12. You can find its code at http://localhost/cgi/widgets3.cgi or www.opensourcewebbook.com/cgi/widgets3.cgi. To prove it works, load one of the URLs.The if part is just like widgets2.cgi, so we won't show it here. The else part is of interest here:
}else{
# if we are here, then we need to build the form
# so print the header, then the html
header(),
start_html(-title => ´An Example of Form Widgets´,
-bgcolor => ´#ffffff´),
h1(´An Example of Form Widgets´),
start_form(),
´Programming language: ´,
textfield(-name => ´language´, -value => ´Perl´),
br(),
´Comments:´,
br(),
textarea(-name => ´comments´,
-cols => 20,
-rows => 5),
br(),
´Rate the coolness: ´,
radio_group(-name => ´coolness´,
-values => [´cool´, ´very cool´, ´ice cold´],
-default => ´ice cold´),
br(),
´I will learn more about Perl and CGI: ´,
checkbox(-name => ´learnmore´,
-value => ´yes´,
-label => ´ Yes´,
-checked => 1),
br(),
´I will use this operating system: ´,
popup_menu(-name => ´operating_system´,
-values => [´Linux´, ´Solaris´, ´HPUX´],
-size => 1),
br(),
´My favorite animal(s): ´,
scrolling_list(-name => ´animal´,
-values => [´Penguin´, ´Camel´, ´Llama´,
´Panther´],
-size => 3,
-multiple => 1),
br(),
br(),
submit(-value => ´Submit Form´),
´ ´,
reset(-value => ´Reset Form´),
end_form();
}
Here the various CGI.pm functions are used to produce the HTML for this form.NNotice that action="/cgi-bin/widgets.cgi" was not set in the start_form() method. CGI.pm is smart enough to set the action to the name of the CGI program it is used within.CGI.pm offers these other methods to create form widgets:
start_multipart_form()
beginning tag for a multipart form
defaults()
tag for a defaults button
password_field()
tag for a password field (a text field containing data that won't be displayedit will be starred-out)
checkbox_group()
tags for a group of check boxes
filefield()
tag for a file upload widget (must use start_multipart_form())
hidden()
tag for a hidden field
image_button()
tag for an image button
button()
tag for a generic button
See the CGI.pm documents for further information.