HTML Forms

An HTML form is used to collect user input. The user input is most often sent to a server for processing.

The <form> Element

The HTML <form> element is used to create an HTML form for user input:

The <form> element is a container for different types of input elements, such as: text fields, checkboxes, radio buttons, submit buttons, etc.

The <input> Element

The HTML <input> element is the most used form element.

An <input> element can be displayed in many ways, depending on the type attribute.

Here are some examples:

Type
Description

<input type="text">

Displays a single-line text input field

<input type="radio">

Displays a radio button (for selecting one of many choices)

<input type="checkbox">

Displays a checkbox (for selecting zero or more of many choices)

<input type="submit">

Displays a submit button (for submitting the form)

<input type="button">

Displays a clickable button

Text Fields

The <input type="text"> defines a single-line input field for text input.

The <label> Element

Notice the use of the <label> element in the example above.

The <label> tag defines a label for many form elements.

The <label> element is useful for screen-reader users, because the screen-reader will read out loud the label when the user focus on the input element.

The <label> element also help users who have difficulty clicking on very small regions (such as radio buttons or checkboxes) - because when the user clicks the text within the <label> element, it toggles the radio button/checkbox.

The for attribute of the <label> tag should be equal to the id attribute of the <input> element to bind them together.


Radio Buttons

The <input type="radio"> defines a radio button.

Radio buttons let a user select ONE of a limited number of choices.

Checkboxes

The <input type="checkbox"> defines a checkbox.

Checkboxes let a user select ZERO or MORE options of a limited number of choices.

The Submit Button

The <input type="submit"> defines a button for submitting the form data to a form-handler.

The form-handler is typically a file on the server with a script for processing input data.

The form-handler is specified in the form's action attribute.

The Name Attribute for <input>

Notice that each input field must have a name attribute to be submitted.

If the name attribute is omitted, the value of the input field will not be sent at all.

The Action Attribute

The action attribute defines the action to be performed when the form is submitted.

Usually, the form data is sent to a file on the server when the user clicks on the submit button.

In the example below, the form data is sent to a file called "action_page.php". This file contains a server-side script that handles the form data:

The Target Attribute

The target attribute specifies where to display the response that is received after submitting the form.

The target attribute can have one of the following values:

Value
Description

_blank

The response is displayed in a new window or tab

_self

The response is displayed in the current window

_parent

The response is displayed in the parent frame

_top

The response is displayed in the full body of the window

framename

The response is displayed in a named iframe

The default value is _self which means that the response will open in the current window.

The Method Attribute

The method attribute specifies the HTTP method to be used when submitting the form data.

The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post").

The default HTTP method when submitting form data is GET.

The HTML <form> Elements

The HTML <form> element can contain one or more of the following form elements:

  • <input>

  • <label>

  • <select>

  • <textarea>

  • <button>

  • <fieldset>

  • <legend>

  • <datalist>

  • <output>

  • <option>

  • <optgroup>

The <input> Element

One of the most used form element is the <input> element.

The <input> element can be displayed in several ways, depending on the type attribute.

The <label> Element

The <label> element defines a label for several form elements.

The <label> element is useful for screen-reader users, because the screen-reader will read out loud the label when the user focus on the input element.

The <label> element also help users who have difficulty clicking on very small regions (such as radio buttons or checkboxes) - because when the user clicks the text within the <label> element, it toggles the radio button/checkbox.

The for attribute of the <label> tag should be equal to the id attribute of the <input> element to bind them together.


The <select> Element

The <select> element defines a drop-down list:

The <option> elements defines an option that can be selected.

By default, the first item in the drop-down list is selected.

To define a pre-selected option, add the selected attribute to the option:

Visible Values:

Use the size attribute to specify the number of visible values:

Allow Multiple Selections:

Use the multiple attribute to allow the user to select more than one value:

The <textarea> Element

The <textarea> element defines a multi-line input field (a text area):

The <button> Element

The <button> element defines a clickable button:

The <fieldset> and <legend> Elements

The <fieldset> element is used to group related data in a form.

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

The <datalist> Element

The <datalist> element specifies a list of pre-defined options for an <input> element.

Users will see a drop-down list of the pre-defined options as they input data.

The list attribute of the <input> element, must refer to the id attribute of the <datalist> element.

The <output> Element

The <output> element represents the result of a calculation (like one performed by a script).

HTML Input Types

Here are the different input types you can use in HTML:

  • <input type="button">

  • <input type="checkbox">

  • <input type="color">

  • <input type="date">

  • <input type="datetime-local">

  • <input type="email">

  • <input type="file">

  • <input type="hidden">

  • <input type="image">

  • <input type="month">

  • <input type="number">

  • <input type="password">

  • <input type="radio">

  • <input type="range">

  • <input type="reset">

  • <input type="search">

  • <input type="submit">

  • <input type="tel">

  • <input type="text">

  • <input type="time">

  • <input type="url">

  • <input type="week">

Input Type Text

Input Type Password

Input Type Submit

The form-handler is typically a server page with a script for processing input data.

The form-handler is specified in the form's action attribute:

Input Type Reset

Input Type Radio

Radio buttons let a user select ONLY ONE of a limited number of choices.

Input Type Checkbox

Checkboxes let a user select ZERO or MORE options of a limited number of choices.

Input Type Button

Input Type Color

Depending on browser support, a color picker can show up in the input field.

Input Type Date

Depending on browser support, a date picker can show up in the input field.

You can also use the min and max attributes to add restrictions to dates:

Input Type Datetime-local

Depending on browser support, a date picker can show up in the input field.

Input Type Email

Depending on browser support, the e-mail address can be automatically validated when submitted.

Some smartphones recognize the email type, and add ".com" to the keyboard to match email input.

Input Type Image

The path to the image is specified in the src attribute.

Input Type File

Input Type Hidden

A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted.

A hidden field often stores what database record that needs to be updated when the form is submitted.

Note: While the value is not displayed to the user in the page's content, it is visible (and can be edited) using any browser's developer tools or "View Source" functionality. Do not use hidden inputs as a form of security!

Input Type Month

Depending on browser support, a date picker can show up in the input field.

Input Type Number

You can also set restrictions on what numbers are accepted.

The following example displays a numeric input field, where you can enter a value from 1 to 5:

Input Restrictions

Here is a list of some common input restrictions:

Attribute
Description

checked

Specifies that an input field should be pre-selected when the page loads (for type="checkbox" or type="radio")

disabled

Specifies that an input field should be disabled

max

Specifies the maximum value for an input field

maxlength

Specifies the maximum number of character for an input field

min

Specifies the minimum value for an input field

pattern

Specifies a regular expression to check the input value against

readonly

Specifies that an input field is read only (cannot be changed)

required

Specifies that an input field is required (must be filled out)

size

Specifies the width (in characters) of an input field

step

Specifies the legal number intervals for an input field

value

Specifies the default value for an input field

You will learn more about input restrictions in the next chapter.

The following example displays a numeric input field, where you can enter a value from 0 to 100, in steps of 10. The default value is 30

Input Type Range

The <input type="range"> defines a control for entering a number whose exact value is not important (like a slider control). Default range is 0 to 100. However, you can set restrictions on what numbers are accepted with the min, max, and step attributes:

Input Type Tel

Input Type Time

Depending on browser support, a time picker can show up in the input field.

Input Type Url

Depending on browser support, the url field can be automatically validated when submitted.

Some smartphones recognize the url type, and adds ".com" to the keyboard to match url input.

Input Type Week

Depending on browser support, a date picker can show up in the input field.

Last updated