19.6. Form Generation

Perhaps you're tired of typing your program's parameter to your browser. Just make a fill-out form instead, which is what most folks are used to. The parts of the form that accept user input are typically called widgets , a much handier term than "graphical input devices." Form widgets include single- and multiline textfields, pop-up menus, scrolling lists, and various kinds of buttons and checkboxes.

Create the following HTML page, which includes a form with one text-field widget and a submit button. When the user clicks on the submit button,[7] the ice_cream script specified in the ACTION tag is called.

[7] Some browsers allow you to leave out the submit button when the form has only a single input text field. When the user types a return in this field, it is treated as a submit request. But it's best to use portable HTML here.

<!-- ice_cream.html -->
<HTML>
    <HEAD>
    <TITLE>Hello Ice Cream</TITLE>
    </HEAD>
    <BODY>
    <H1>Hello Ice Cream</H1>
    <FORM ACTION="http://www.SOMEWHERE.org/cgi-bin/ice_cream">
    What's your flavor? <INPUT NAME="favorite" VALUE="mint">
    <P>
    <INPUT TYPE="submit">
    </FORM>
    </BODY>
</HTML>

Remember that a CGI program can generate any HTML output you want, which will then be passed to any browser that fetches the program's URL. A CGI program can, therefore, produce the HTML page with the form on it, just as a CGI program can respond to the user's form input. Moreover, the same program can perform both tasks, one after the other. All you need to ...

Get Learning Perl, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.