CGI and Perl

Text Fields

One of the most commonly used fields is the text field. This is an input field that allows the user to type in a single line of text. For multiple lines of text see "Text Areas" in the following section. Text fields can be used for things such as names, phone numbers, and other user input. Using raw HTML, you can create a text field using the INPUT tag with TYPE text as shown here:

<INPUT TYPE=text NAME="Name"" SIZE=32 MAXLENGTH=32 VALUE="Your name">

Using CGI::Form, this is a call to the method textfield, as shown here:

$q->textfield(-name=>'Name', -default=>'Your name', -size=>32, -maxlength=>32);

-name specifies the name of the field. This value is used later for querying the value of the field during the POST request. -default specifies the default value of the field. -size specifies the size, in characters, of the displayed text field. -maxlength specifies the maximum number of characters allowed for the field.

Text Areas

Text areas are very similar to text fields. The difference is that text area values can span multiple lines. Text areas might be used for such things as addresses or comments. Using raw HTML, you can create a text area using the <TEXTAREA> tag as shown here:

<TEXTAREA NAME="Address" ROWS=6 COLS=32>Your address</TEXTAREA>

Using CGI::Form, this is a call to the method textarea, as shown here:

$q->textarea( -name=>'Address', -default=>'Your address', -rows=>6, -cols=>32);

-rows specifies the number of rows to display to the user. -cols specifies the number of columns to display to the user. The user is not constrained to the values of -rows and -cols for his/her entry. If the user needs extra space to enter more text, he/she can use the scrollbars provided with this form field.

Password Fields

Password fields are also very similar to text fields. The difference is that the characters that are typed into the field are not displayed. This is useful for entering passwords (thus the name password field). Using raw HTML, you can create password fields using the INPUT tag with TYPE password like this:

<INPUT TYPE=password NAME=password VALUE="" SIZE=8 MAXLENGTH=8>

Using CGI::Form, this is a call to the method password_field, as shown here:

$q->password_field( -name=>'Password', -default=>'', -size=>8, -maxlength=>8 );

Radio Buttons

Radio buttons are used to select one out of several choices. This can be used for such things as designating a sex or marital status. Using raw HTML, you create radio buttons using the INPUT tag with TYPE radio. The CHECKED attribute can be used to specify which button to initially highlight. The following example shows how to highlight the button for selecting "male".

<INPUT TYPE=radio NAME='sex' VALUE='male' CHECKED>
 <INPUT TYPE=radio NAME='sex' VALUE='female'>

Because radio buttons are generally created in groups, CGI::Form provides a single interface for creating a group of radio buttons. This method is called radio_group, as shown here:

$q->radio_group( -name=>'sex', values=>[`male', `female'], -default=>'female',
                  -linebreak=>'true' );

The -linebreak option causes the buttons to be laid out vertically aligned.

by BrainBellupdated
Advertisement: