Lesson 7 | Widgets |
Objective | Learn about specific widgets and what they do. |
CGI.pm FORM defaults
During the dotcom era, components in web development using Perl, especially in the context of Perl/CGI-based frameworks, were often referred to as "widgets." This terminology was particularly common in web application frameworks and templating systems that Perl developers used to build dynamic user interfaces.
The term "widget" was borrowed from graphical user interface (GUI) development, where it referred to elements like buttons, text fields, and menus.
Frameworks like Perl's *HTML::Widgets* module provided ways to create and manage these UI components, which were called "widgets" for familiarity and ease of understanding.
The different
FORM
elements and the widgets the browser creates from them were listed in the first lesson of this module.
In this lesson, we will look at each of them in sufficient detail for you to make use of their various features.
Optional features for the widgets are accessed via attributes in the HTML elements.
For example, to set the size of a text field, you would use the
SIZE
attribute in the HTML like this:
<INPUT TYPE=TEXT SIZE=35>
This would give you a text field with enough space to type 35 characters. Keep in mind that most of these optional features do not work the same on different browsers.
I have put together a list of widgets and their attributes.
You will need to study this list before you complete this lesson's exercise. So, without further ado:
Widgets List
NAME |
The name of the group this radio button belongs to |
VALUE |
The value that will be sent if the button is checked |
CHECKED |
If present, this is the default button. Only one button in a group should have this attribute. |
<INPUT TYPE=SUBMIT>
This widget is the submit button for the form. A form may have more than one submit button.
You will be able to determine which one was clicked by giving them different names.
VALUE | Used to set the text that is displayed in the button |
<INPUT TYPE=RESET>
The
RESET
widget is a button that resets the form. It does not generate a name/value pair.
VALUE |
Used to set the text that is displayed in the button |
<INPUT TYPE=HIDDEN>
The
HIDDEN
field is used for sending informational data to the server. It is not displayed, but its name/value pair gets sent
to the server along with the form. This is commonly used for maintaining context (we'll use this later in this module).
VALUE
|
The value sent with the name/value pair
|
<INPUT TYPE=IMAGE>
This is a graphical version of the
SUBMIT
widget. It is commonly used for creating graphical buttons.
When you click the image, it submits the form along with the coordinates of where the image was clicked. This widget generates
two name/value pairs like this:
name.x=35
name.y=42
NAME | The name for the name/value pairs. Note the description above for this widget's unique behavior. |
SRC | The URL of the image to display |
WIDTH | The width of the image |
HEIGHT | The height of the image |
BORDER | Defaults to having a border. Use BORDER=0 to turn it off. |
VALUE | There is no VALUE attribute for this widget! |
<SELECT>
</SELECT>
A widget that displays multiple choices, like a dropdown box or a selection box. This element is an HTML container, so it requires an end tag. Within the container, each selection option is indicated with an
<OPTION>
tag. For example:
<SELECT NAME="parts" SIZE=4 MULTIPLE>
<OPTION> Strings
<OPTION SELECTED> Pickups
<OPTION> Bridge
<OPTION> Neck
<OPTION> Frets
<OPTION> Tuning machines
</SELECT>
Use the
SELECTED
attribute in the
OPTION
tag to indicate default selections.
SIZE |
The number of options displayed at once. Omit this for a drop-style box on some systems. |
MULTIPLE |
Include this attribute to allow multiple selections. |
<TEXTAREA>
</TEXTAREA>
A free-form text-entry area. Note that the
WRAP
attribute is a common Netscape extension.
ROWS |
The height of the widget's display area in lines of text |
COLS |
The width of the widget's display area in columns of characters |
WRAP |
These are possible values:
HARD enables word wrap and passes the line breaks to the server.
SOFT enables word wrap and does not pass the line breaks to the server.
OFF disables word wrap.
|
CGI.pm FORM defaults
CGI.pm has very reasonable defaults for these form elements. These are as follows:
attribute |
default |
method |
method="POST" |
action |
action="myscript.cgi" or whatever your current cgi script is called. |
enctype |
enctype="application/x-www-form-urlencode" |
We should not ever need to change the enctype within our form (in fact we should avoid even trying to do so).
If you want to create a multi-part form (for uploading files etc) we create this in a different manner. We must use this syntax for multi-part forms if we want CGI.pm to handle them properly. We create a multi-part form like this:
print start_multipart_form({-action=>"myscript.pl", -method=>"POST"});
# print form internals.
print end_form();
Perl Widget - Exercise