7    HTML Forms and Interactive Elements

Nowadays, hardly any major web presence can do without forms (also referred to as HTML forms or web forms), in which visitors can fill out input fields or select something from a list of specific entries, such as a survey form.

Common usage scenarios for HTML forms include contact forms, surveys, signing up for a website or newsletter, guest books, order forms, search functions, adding and uploading data, and more. For such interactions, HTML provides you with various input fields such as text input fields, dropdown lists, radio buttons, and simple buttons.

In this chapter, you’ll learn the following:

HTML Element

Description

<form>

Defines a space for forms

<fieldset>

Groups form elements

<legend>

Defines a heading for a fieldset element

<label>

Adds and links a text label to form elements such as input fields, radio buttons, and checkboxes

<datalist>

Provides a list of option values that can be used as a suggestion list

<input>

Requests data from the user via many different types

<button>

Defines a clickable button to trigger actions

<select>

Initiates a selection list for which you can create an entry of using the option element

<optgroup>

Allows you to group option elements of a selection list

<option>

Defines an entry of a selection or dropdown list

<textarea>

Defines an area for entering multiline text

<output>

Defines an area for outputting values, and is the counterpart of the input element

<progress>

Allows you to define an area for the visual representation of a progress

<meter>

Visualizes the size of a value

Table 7.1     Quick Overview of the Elements Covered Here for HTML Forms

HTML Element

Description

<details>
<summary>

Expands and collapses page content

<dialog>

Displays a dialog box

Table 7.2     Quick Overview of the Interactive Elements Covered Here

7.1    Defining a Space for Forms

You mark an area for an HTML form with the opening <form> and closing </form>. Everything you write in between the two tags will be part of the form. In practice, various HTML input fields, such as text input fields, selection lists, radio buttons, and labels can be used here.

The basic structure of a typical HTML form could therefore look as follows:

<form action="/php/feedback.php" method="post">
<!--Form elements for the feedback-->
</form>

Here you see two typical attributes—action and method—which are used quite often with HTML forms. The meaning of the individual attributes is as follows:

You don’t necessarily need to use the two attributes action and method in the <form> tag. The attributes are unnecessary, for example, if you want to process the entered data in the form only with JavaScript, for example, to perform simple calculations. For such purposes, you don’t need a URL to be called or an HTTP request method.

Note that the preceding listed attributes weren’t all the existing attributes for the form element. Other attributes you should know are listed here: