Working With Forms: The Form Element
Working With Forms: The Form Element
In addition to being a container for form control elements, the form element has some attributes that are necessary for interacting with the form-processing program on the server. Lets take a look at each. The action attribute provides the location (URL) of the application or script (sometimes called the action page) that will be used to process the form. The ACTION value is required for working forms, though on some browsers a value similar to the base URL of the document will be assumed if the ACTION is left out. The method attribute specifies how the information should be sent to the server. There are only two methods for sending this encoded data to the server: POST or GET indicated using the method attribute in the form element.
Add Input controls to the Form Lets discuss some input controls, or ways for users to enter data. For example, for you to enter your name on a form, there must be a space for you to do so. This space is what Im referring to when I use the phrase input control or simply control. Figure 2 contains some input controls that are labeled for you.
Figure 2: An example of a basic input controls The majority of these controls are created with an input tag, the actual description of which control you want to include is made through the type attribute, as in the following example: <form> <input type="text" /> </form> The next sections explain the specific types of controls created with the input tag, as well as a few others that arent created by it.
<form> <input type="text" name="First Name" value="Enter your Name" /> <input type="password" name="pswd" /> <textarea name="contest_entry" rows="5" cols="100" > Tell us why you love the band in 50 words or less. Five winners will get backstage passes! </textarea> </form>
Radio buttons
Radio buttons are added to a form with the input element with the type attribute set to radio. The name attribute is required. Here is the syntax for a minimal radio button: In this example, radio buttons are used as an interface for users to enter their age group (a person cant belong to more than one age group, so radio buttons are the right choice). Figure 4 shows how radio buttons are rendered in the browser. <form> <input type="radio" <input type="radio" <input type="radio" <input type="radio"
value="under24" checked="checked" /> under 24 value="25-34" /> 25 to 34 value="35-44" /> 35 to 44 value="over45" /> 45+
Checkbox buttons
Checkboxes are added using the input element with its type set to checkbox. As with radio buttons, you create groups of checkboxes by assigning them the same name value. The difference, as weve already noted, is that more than one checkbox may be checked at a time. The value of every checked button will be sent to the server when the form is submitted. Here is an example of a group of checkbox buttons used to indicate musical interests. Figure 4 shows how they look in the browser: <form> <input <input <input <input type="checkbox" type="checkbox" type="checkbox" type="checkbox" name="genre" name="genre" name="genre" name="genre" value="punk" checked="checked" /> Punk rock value="indie" checked="checked" /> Indie rock value="techno" /> Techno value="rockabilly" />Rockabilly
</form>
Menus
Option for providing a list of choices is to put them in a pull-down or scrolling menu. Menus tend to be more compact than groups of buttons and checkboxes. You add both pull-down and scrolling menus to a form with the select element. Whether the menu pulls down or scrolls is the result of how you specify its size and whether you allow more than one option to be selected.
Pull-down menus
The select element displays as a pull-down menu and its a container for a number of option elements. The content of the chosen option element is what gets passed to the web application when the form is submitted. In pull-down menus, only one item may be selected. Heres an example
<select name="EightiesFave" id="form-fave"> <option>The Cure</option> <option>Cocteau Twins</option> <option>Tears for Fears</option> <option>Thompson Twins</option> <option>Everything But the Girl</option> <option>Depeche Mode</option> <option>The Smiths</option> <option>New Order</option> </select>
Scrolling menus
To make the menu display as a scrolling list, simply specify the number of lines youd like to be visible using the size attribute. This example menu has the same options as the previous one, except it has been set to display as a scrolling list that is six lines tall. The multiple attribute allows users to make more than one selection from the scrolling list. Use the selected attribute in an option element to make it the default value for the menu control. <select name="EightiesBands" size="6" multiple="multiple" for="EightiesBands"> <option>The Cure</option> <option>Cocteau Twins</option> <option selected="selected"> Tears for Fears</option> <option selected="selected"> Thompson Twins</option> <option>Everything But the Girl</option> <option>Depeche Mode</option> </select>