CS 4, Summer 2006: Lecture 5: HTML Forms

Overview

Forms

How do we get information from the user (and know what that information is about)?


Example: Application to Cross the Bridge of Death

Stop! Who would cross the Bridge of Death must completely fill out this simple form, in triplicate, and have it signed by his or her supervisor, ere the other side (s)he see!

What is your name?

What is your quest?

What is your favourite colour?
Red
Green
Blue
Yellow
I don't know!

What kinds of animals do you like?

Swallows Elephants Aardvarks
Cows Rabbits Dragons

What is your nationality?

What is the airspeed velocity of an unladen swallow?


Input tag

Most of the input elements in the example form are generated using the <input> tag. In general, the form of an input tag is as follows:

<input type="type" name="name" value="value">

The type attribute specifies what kind of input element it will be. Some of the common types include:

Names

In order to refer to an input element, you need to give it a name, which is just some string of characters. The name attribute is used to assign a name to an input element. While it is not required that you give each input element a name, it is recommended, since without a name, you won't have any way to refer to the input element when you actually go to use it.

The name attribute is also important for radio buttons -- all the radio buttons which share a common name value are considered part of the same "button group", for the purposes of figuring out which buttons should pop out when you select one of them.

Other Input Elements

There are two other input elements worth noting in the above example. These are the <select> tag, which was used for asking about your nationality, and the <textarea> tag, which is used to obtain text inputs with multiple lines (the nature of your quest).

Doing something with the values

Later this term, we'll see how to make a web page dynamic, reacting to the values entered in forms, button presses, etc. With that JavaScript-based approach, all the processing is done on your PC, within the browser. An alternative, which we won't cover beyond the above example, is to submit the form data to the web server, to be handled by a program there. (This is largely the way your interactions with on-line stores and so forth are handled. However, Google Maps and other such dynamic sites are a kind of hybrid, combining server-based and browser-based processing.)

For server-based processing, we need to specify the form's method attribute (can be "post" or "get"; not important here) and its action attribute (the URL of the program that will process the form data). For illustration purposes, the example form is processed by a program that simply echoes the input values.