Creating Forms

You can create forms with CGI.pm using functions that have obvious names.

A Simple Form


#!/usr/bin/perl
# file: final_exam.pl

use CGI ':standard';

print header;
print start_html('Your Final Exam'),
    h1('Your Final Exam'),
    start_form,
    "What's your name? ",textfield(-name=>'first_name'),
    p,
    "What's the combination?",
    p,
    checkbox_group(-name    =>  'words',
		   -values  =>  ['eenie','meenie','minie','moe'],
		   -defaults => ['eenie','minie']),
    p,
    "What's your favorite color? ",
    popup_menu(-name   => 'color',
	       -values => ['red','green','blue','chartreuse']),
    p,
    submit,
    end_form,
    hr;

if (param()) {
    print 
	"Your name is: ",param('first_name'),
	p,
	"The keywords are: ",join(", ",param('words')),
	p,
	"Your favorite color is: ",param('color'),
	hr;
}

print end_html;

http://your.site/cgi-bin/final_exam.pl

Form-Generating Functions

Run perldoc CGI for details:

start_form
Start the form (returns the <form> tag with default attributes).

end_form
End the form by returning the </form> tag.

textfield(-name=>$name,-value=>$starting_value)
Create a text field.

password_field(-name=>$name,-value=>$starting_value)
Create a password field.

textarea(-name=>$name,-value=>$starting_value,-rows=>$rows,-cols=>$cols)
Create a multiline text input area.

checkbox(-name=>$name,-value=>$value,-checked=>1)
Create a single checkbox.

checkbox_group(-name=>$name,-value=>\@values,-default=>\@on)
Create a group of checkboxes sharing the same name. @values gives the list of checkbox values, and @on gives the list of those that are initially on.

radio_group(-name=>$name,-value=>\@values,-default=>$on)
Create a group of radio buttons sharing the same name. @values gives the list of radio values, and $on indicates which one is on to start with.

popup_menu(-name=>$name,-value=>\@values,-default=>$on)
Create a popup menu. @values gives the list of items, and $on indicates which one is initially selected.

scrolling_list(-name=>$name,-value=>\@values,-default=>$on)
Create a scrolling list. @values gives the list of items, and $on indicates which one (if any) is initially selected.

submit(-name=>$name,-value=>$value)
Creates a submit button. $value optionally sets the button label.


<< Previous Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Fri Oct 20 11:55:47 EDT 2000