1. Home
  2. Computing & Technology
  3. Perl

Working with CGI.pm In-Depth

Creating Text Areas

From About.com

We've already seen how to make a basic text field with CGI.pm, so now let's take a look at creating the multi-row text areas. Most of the basic attributes are the same, and of course the text area has all the attributes detailed in the general form attributes section. Let's take a look at an example:
print $cgi->textfield(
-name => 'blog_entry',
-id => 'blog_entry',
-default => 'Write something here.',
-rows => 5,
-cols => 55 );
There are several attributes that are unique to the text area. The first is the -rows attribute, which (unsurprisingly) is used to set the number of rows you want the field to display. -cols will set the number of visual columns, which in practice is similar to the -size attribute on the text field. These are purely visual settings and do not change the allowed length of any data entered in the field itself. The -default setting can be used to set a value for the content of the text area.

Here is what the above example produces in plain HTML:

<textarea name="blog_entry" rows="5" cols="55" id="blog_entry">Write something here.</textarea>
A couple of other attributes you might find useful are -disabled and -readonly. Disabling a text area means that the field will refuse focus or changes - typically it will be greyed out - and will be skipped in the tab index. Marking a text area as read only means that the field can still be selected, but the contents will not be editable.

Here's a sample using the disabled attribute:

print $cgi->textfield(
-name => 'blog_entry',
-id => 'blog_entry',
-default => 'Write something here.',
-disabled => 'disabled',
-rows => 5,
-cols => 55 );
And an example with read only set:
print $cgi->textfield(
-name => 'blog_entry',
-id => 'blog_entry',
-default => 'Write something here.',
-readonly => 'readonly',
-rows => 5,
-cols => 55 );

Explore Perl

More from About.com

  1. Home
  2. Computing & Technology
  3. Perl
  4. CGI & Web
  5. Working with CGI.pm In-Depth - How to build a text area with CGI.pm

©2008 About.com, a part of The New York Times Company.

All rights reserved.