Forms



index
Disabled back button Next Section
printable version

Section 1: Overview

Forms are an inescapable part of web design and development.

A form is a web page, or a segment of a page, that contains special elements called form controls and labels for those controls.

A form element (<form></form>) acts as a container for form controls.

The form element specifies

Form controls make up the user interface for the form.

When a user fills out an HTML form, he/she enters information or makes selections using form controls.

Here is a link to the HTML5 specifications on forms and controls.


Form Handlers

Unfortunately, once you start working with HTML forms, you quickly discover that while you can design a form using any HTML editor, you can't make it do anything with HTML.

In order to make a form function as desired, you must have an HTML form handler.

What the form handler does depends on the nature and complexity of the HTML form.

The form handler is NOT part of the HTML, it is a separate program which must be installed on your webserver.

Section 2: Examples

When the user submits the form (usually by clicking a Submit button), the data is collected from the form and sent to a server, as specified by the form's action, for some sort of form processing. In some cases that form data is manipulated locally by JavaScript.


The first example is an html form that calls a form handler on the server, implemented in a PHP function, to read the user's name and echo it.

Here is a link, and the html code for the form:

<body>
<form action="welcome.php" method="post">
<!--
Notice I use the name attribute here. It is valid for use
with form controls, and the underlying PHP code needs it.
-->
<div>
<label for="firstname">First Name:</label>
<input type="text" placeholder="Enter first name..." name="firstname" >
</div>
<label>
Last Name:
<input type="text" placeholder="Enter last name..." name="lastname" >
</label>
<input class="newLine" title="Click to process the data." type="submit" value="Go!" >
</form>
</body>

Here is the php file for anyone who is interested.



Here is another example, but this time it uses a JavaScript form handler.

The form code appears below, and here is a link. Clicking View Page Source will reveal the HTML code and the JavaScript form handler.

<body>
<form>
<h2>MPH to KPH Converter</h2>
<label for="txtMPH">Enter MPH:</label>
<!--
Notice that I used the id attribute to assign an identifier to
the field. We'll use it later.
-->
<input autofocus id="txtMPH" class="text1" type="text">
<!--
Notice the button's onclick event. It calls the calcKM function
when the button is clicked.
-->
<input id="cmdCalc" type="button" onclick="calcKM();" value="Convert" >
<label for="txtKPH" class="label1">KPH:</label>
<!--
Notice that I used the id attribute to assign an identifier
to the field.
-->
<input id="txtKPH" class="text2" type="text" readonly ="readonly" >
</form>
</body>

Section 3: Form Submission

Web forms are mostly used to capture user entered data and post them back to the server.

To let the browser know where to send the content these properties can be added to the <form> tag:

When the user clicks on the "Submit" button, the content of the form is sent to another file.

Form processing will be covered in more detail in INFO 4430.


Example

The demo in Section 2 (link) was actually written for use in INFO 4430, and uses a Submit button to process user input.


Overriding Form Methods

The formmethod attribute can be associated with form controls to override the method attribute on the form element.

The formaction attribute acts similarly.

Section 4: Original Form Tags

Form fields are objects that allow the visitor to enter information - for example text boxes, drop-down menus or radio buttons.

Prior to HTML5, most controls were differentiated by the type attribute of the input element.

Some controls, like textareas, have their own tag:

<textarea rows="5" cols="20">A lot of text here</textarea>

Select fields also have their own tag:

<select size="2">
<option value="first option">Option 1</option>
<option value="second option">Option 2</option>
<option value="third option">Option 3</option>
</select>


Control Choices

The following controls have been available in prior versions of HTML and can be added to your forms. Click any control for a pre-HTML5 example and details.

Here are examples of each of the following controls. View Source for code examples.

Original Form Fields
(Click a row for details)
Type Format HTML5 Spec
Text field <input type="text" > link
Text fields are one line areas that allow the user to input text.
HTML Code:
<label> Example text field: <input type="text" name="example" > </label>
Password field <input type="password" > link
Password fields are similar to text fields, with the difference being that what is entered into a password field shows up as dots on the screen in order to prevent others from reading the password on the screen.
HTML Code:
<label> Example password field: <input type="password" name="pw" > </label>
Hidden field <input type="hidden" name="course" value="INFO2220" > link
Hidden fields are not displayed on the page so the visitor can't type anything into the field. Hidden fields are used to submit information that may not be user provided, such as the page name that the form data came from or the email address to which the form should be posted.
HTML Code:
<label> Example hidden field: <input type="hidden" name="course" value="INFO2220"> </label>
Radio buttons <input type="radio" > link
Radio buttons are used when you want to let the visitor select one - and just one - option from a set of alternatives. The name attribute is used to group the radio buttons together. (image)
  • The id of each radio button is unique. Each id on a web page must be unique, and the id elements of each radio button follow those rules.
  • Each radio element also has a name attribute. The name attribute is used to specify the entire group of radio objects.
  • All radio buttons in a group have the same name. HTML uses the name attribute to figure out which group a radio button is in, and to ensure that only one button in a group is selected.
HTML Code:
<label> Example radio buttons:
<label><input type="radio" name="radioSet1" value="INFO 1181" checked>INFO 1181</label>
<label><input type="radio" name="radioSet1" value="INFO 2220">INFO 2220</label>
<label><input type="radio" name="radioSet1" value="INFO 3301>INFO 3301</label>
</label>
Check Boxes <input type="checkbox" name="check1" id="option1" value="INFO2220" > link
Check boxes are used when you want to let the visitor select one or more options from a set of alternatives.
HTML Code:
<label> Example check boxes:
<label>
<input type="checkbox" id="option1" name="check1" value="INFO 1181" checked>INFO 1181
</label>
<label>
<input type="checkbox" id="option2" name="check1" value="INFO 2220" checked>INFO 2220
</label>
<label>
<input type="checkbox" id="option3" name="check1" value="INFO 3301>INFO 3301
</label>
</label>
File input field <input type="file" > link
The file input field creates a field through which users can upload files from a local computer or network.
  • If PHP is used to process the form, the MAX_FILE_SIZE hidden field (measured in bytes) must precede the file input field, and its value is the maximum filesize accepted by PHP. (link)
HTML Code:
<label> Example file input field:
<input type="hidden" name="MAX_FILE_SIZE" value="30000" >
<input type="file" name="selectedfile" >
</label>
Button field <input type="button" > link
Input fields with the button type define clickable buttons that are used primarily with JavaScript to activate a script. We will later see that there is a button tag as well.
HTML Code:
<label> Example button field: <input type="button" value="Example Button" > </label>
Submit button field <input type="submit" > link
The submit button field is a button that gives the user the option of submitting the form. The value parameter is used to specify the text that appears on the button. When a visitor clicks a submit button, the form is processed according to the form-submission action for the element specified in the action setting (or formaction setting in HTML5) of the <form< tag.
HTML Code:
<label> Example submit button field: <input type="submit" value="Submit Type" > </label>
Reset button field <input type="reset" > link
The reset button field is a button that will reset the form fields to their default values with no additional coding required. The button text is specified using the value parameter.
HTML Code:
<label> Example reset button field: <input type="reset" value="Reset Type" > </label>
Image button field <input type="image" > link
Image button fields have the same effect as submit buttons. When a visitor clicks an image button, the form is processed according to the form-submission action for the element specified in the action setting (or formaction setting in HTML5) of the <form> tag. These also require a src attribute, like the img tag.
HTML Code:
<label> Example image button field: <input type="image" alt="image button" src="demos/cisDark.jpg" > </label>
Text areas <textarea cols="x" rows="y"></textarea> link
Text areas are text fields that can span several lines. You can specify how many columns and rows you want in your text area via the cols and rows settings. The user can enter info into the field. If you specify text between the start and end tag, everything written between these tags will be presented in the text area box.
HTML Code:
<label>
Example text area:
<textarea cols="25" rows="5" id="textarea">You can enter several lines here.</textarea>
</label>
Drop down (select) <select> </select> link
Button tag <button> </button> link
The HTML button tag is used for creating a button within forms.
Although you can also use the <input> tag to create an HTML button, the <button> tag allows you to place HTML between the <button> </button> tags, which enables you to do things you wouldn't normally be able to with the <input> tag. You can specify the type of the button (submit, reset, button) by using the type attribute.
HTML Code:
<label> Example button field:
<button value="Button Example>Button Element</button>
</label>

Note: To specify the maximum number of characters allowed in the <input> element, use the maxlength attribute. (If the attribute is omitted, then there is no maximum allowed value length.)


Helpful Links

Section 5: Labels

Another aspect of forms that needs to be addressed is labels for form fields. Let's look again at the first example from Section 2:

Notice the labels for the first name and last name input fields.

The label element represents an on-form caption for a form control, and is used to identify or to associate a description with a form control.

Each label element can only be associated with a single form control, while a form control may have several labels associated with it.

In the html form code below, the labels are highlighted:

<body>
<form action="welcome.php" method="post">
<div>
<label for="firstname">First Name:</label>
<input autofocus type="text" placeholder="Enter first name..." name="firstname" >
</div>
<label>
Last Name:
<input type="text" placeholder="Enter last name..." name="lastname" >
</label>

<input title="Click to process the data." type="submit" value="Go!" >
</form>
</body>

The label element associates a label with a form control. By associating labels with form controls, authors give important hints to users of speech browsers while also allowing visual browsers to duplicate common GUI features.

Each label element is associated with exactly one form control.

The for attribute explicitly specifies the control associated with the label.


Helpful Links

Section 6: New Elements

HTML5 introduced 5 new elements related to input and forms.

Here are some examples:

Section 7: New Input Types

HTML5 includes a variety of new input types to help improve accessibility for browsers and assistive technologies.

Section 8: New Input Attributes

New Input Attributes

HTML5 also introduced several new attributes for the input and form elements.

You can read about all of them here, but we'll emphasize a few of them.

autofocus
placeholder
pattern
Others

You can find out more about attributes in HTML5 tutorial- New form attributes.

Section 9: Control Groupings

<fieldset>

The <fieldset> tag is used to group related elements in a form, and draws a box around the related elements.

<legend>

The <legend> tag defines a caption for the <fieldset> element.

Note: there should be no tags between the <fieldset> tag and the <legend> tag.

Here is an HTML - Fieldsets and Legends video and an HTML <fieldset> tutorial and example..

Here is a version of the demo page that we looked at earlier, but this time using fieldsets and legends rather than divs and labels: link


Formatting Tip

It is not uncommon to see forms with multiple form fields that appear on the same line, but setting up the CSS for these forms can be a challenge because the display property of many form fields defaults to block.

Here is an example that demonstrates the use of inline-block to format the form fields correctly. Be sure to View Page Source and look at the CSS.

Section 10: Extra Notes
Section 11: Resources

Accessible Rich Internet Applications