1. Home
  2. Computing & Technology
  3. Perl

Working with CGI.pm In-Depth

Creating Text Fields

By Kirk Brown, About.com

Creating a basic text field for your form is simple with CGI.pm. In fact, most of the attributes for the textfield are described in the general form attributes section. The two text field specific attributes are -size and -maxlength. Here's an example:
print $cgi->textfield(
-name => 'first_name',
-id => 'first_name',
-value => 'Bob Smith',
-size => 55,
-maxlength => 128 );
As you can see, the first three attributes are pulled from the the general form attributes section - remember that those form attributes can be used in any of the form field types. The first new one we'll look at is -size. This translates to it's HTML equivalent size="55" which sets the actual character width of the form field itself. This is purely a visual setting and will not change how many characters the field uses.

-maxlength however, sets a cap on the number of characters allowed in a specific field. In the above example, the HTML equivalent is maxlength="128", which will disallow a form value of more than 128 characters. One thing to remember about this, is that it only sets the maxlength on the interface itself. This could be bypassed by directly submitting a POST to the processing script. Do not use maxlength as a replacement for proper input validation on the server side.

Here is what the above example produces in plain HTML:

<input type="text" name="first_name" value="Bob Smith" size="55" maxlength="128" id="first_name" />

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 field with CGI.pm

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

All rights reserved.