next up previous
Next: About this document Up: No Title Previous: No Title

HTML and CGI

1. Making a web form

On an HTML page, you can have a ``form''. The user fills out the form and then submits it to your CGI program. A form can have one or more of the following kinds of fields.

a text field
, a rectangle the user can fill in with text

a multiline text field
(text area), with a larger box that scrolls

a checkbox
that can be ``toggled'' on and off by clicking on it. You might have several of these side-by-side; the user can select one or more of them.

a group of radio buttons
, where only one at a time can be selected. Clicking on a new selection undoes the old selection.

a menu
, where you see one selection at a time in a rectangle until you click on it, and then you can select an item. You can set a menu so only one item at a time can be selected, or you can set it so multiple items can be selected.

a scrolled list
, which is like a menu where you can see several items at once, with a scroll bar.

a password field
, which doesn't show what the user is typing. Even so, all the browser does is transmit the information typed; it doesn't do any encryption.

a submit button
, which when clicked on causes the browser to send the user information to your CGI program

a reset button
, which clears the form and resets default values.

a hidden field
, which can be used for storing information in the form to send to your CGI program. The user doesn't see it. We'll discuss later why you'd want to do that.

There are two methods of transmitting the information to your CGI program:

Of the two methods, ``POST'' seems more natural, but with ``GET'' it can be easier to test your program. The ``GET'' method is often used for small amounts of data and the ``POST'' method for larger amounts.

2. Form tags

Suppose you want to make a questionnaire about some product, asking users for ``your name'', ``your email address'', and ``your opinion''. The first two can be text input and the third, multiline. Also suppose you have a checkbox for ``student'', radio buttons for ``new customer'' or ``repeat customer'', a menu for US states, and a scrolled list accepting multiple choices for ``kinds of computers used''. Also, suppose your script for processing the form is get_info.cgi and you are using the ``GET'' method. Your form will use tags <FORM ...> and </FORM> with other possible tags in between, such as <INPUT> and <SELECT> .

There are also attributes that go with the opening tags. One of these attributes is usually NAME="..." , which is a name you give the field. The user's action will give that field a value. When the form is submitted to your CGI program, the field name and value are transmitted together so your program can tell what the value is for.

Some fields also have an attribute VALUE="..." . This specifies a default value to be used if the user doesn't give a value. You can omit this field if you don't want a default value.

Notes:

  1. In theory there could be more than one form on a page, but usually there's just one. The reason is that only one is submitted when the user clicks on its ``submit'' button, which results in the others being ignored.
  2. To lay out the fields on your form, you can use controls such as breaks <BR> or lists or even tables. Tables can be good for keeping things lined up.
  3. Naturally, you will have explanations interspersed with the requests for user information.

3. A sample form

Here's an example of the concepts above, but including only four US states. To see the actual form, point your browser to
http://www.math.ucla.edu/~baker/40/sample_form.html . At this writing, there is no corresponding CGI script in place, however.

[usual HTML header]
<BODY>
<H1> Questionnaire about our product </H1>

<FORM ACTION="get_info.cgi" METHOD="GET"> 

<TABLE>
<TR>
    <TD> Your name:
    <TD> <INPUT TYPE="text" NAME="name" SIZE=30 MAXLENGTH=50>

<TR>
    <TD> Your email address:
    <TD> <INPUT TYPE="text" NAME="email" SIZE=30 MAXLENGTH=50>
</TABLE>

<UL>
<LI> Your opinion of our product: <BR>
<TEXTAREA ROWS=10 COLS=50 NAME="opinion">
</TEXTAREA> <BR>

<LI> Check if you are a student:
<INPUT TYPE="checkbox" NAME="student"> <BR>

<LI> Are you a new user
<INPUT TYPE="radio" NAME="oldornew" VALUE="new" CHECKED>
or a repeat user
<INPUT TYPE="radio" NAME="oldornew" VALUE="repeat">
(select one) <BR>

<LI> Your state:
<SELECT NAME="state" SIZE=1>
   <OPTION SELECTED> AK
   <OPTION> AL
   <OPTION> CA
   <OPTION> TX
</SELECT> <BR>

<LI> The computer system(s) you use (select one, or more
by holding down the CTRL key): <BR>
<SELECT NAME="computer" SIZE=4 MULTIPLE>
   <OPTION SELECTED> PC with Win 95/98
   <OPTION> PC with Win NT
   <OPTION> Macintosh
   <OPTION> UNIX system
</SELECT> <BR>
</UL>

<P>
<INPUT TYPE="submit" VALUE="Submit the form"> <BR>
<P>
<INPUT TYPE="reset" VALUE="Clear the form"> <BR>
</FORM>
</BODY></HTML>

4. What's next?

The next step is to learn how to read the user information with a CGI program.


next up previous
Next: About this document Up: No Title Previous: No Title

Kirby A. Baker
Mon Feb 15 12:54:02 PST 1999