HTML form

On this page 7

An HTML form is a section of a web document, defined by the form element, that groups interactive controls such as text fields, checkboxes, radio buttons, select menus and buttons in order to collect user input and submit it for processing. In HTML, forms are the native mechanism behind sign-ins, searches, checkouts, registrations and contact pages.

A form specifies where the data goes with the action attribute and how it is sent with the method attribute. When submitted, the browser gathers every named, enabled control into name-value pairs and sends them as an HTTP request. Modern HTML5 adds built-in validation, specialized input types and APIs that let scripts enhance the behavior without replacing it.

How an HTML form submits data

Submission follows a defined algorithm. The browser validates the controls, builds the form data set from every control with a name attribute, encodes it, and sends it to the URL in action. Disabled controls and controls without a name are excluded from submission, which is a frequent source of missing data.

The key attributes of the form element control this process, as summarized in the table.

AttributeTypical valuesEffect
actionA URL, defaults to the current pageDestination of the submission
methodget, post, dialogHTTP method or dialog closing
enctypeapplication/x-www-form-urlencoded, multipart/form-data, text/plainEncoding of the request body
novalidateBooleanDisables built-in constraint validation
autocompleteon, offDefault autofill behavior for controls
target_self, _blank, a frame nameWhere the response is displayed
Key attributes of the form element

The choice between GET and POST matters. GET appends data to the URL as a query string, which suits searches and filters that should be bookmarkable. POST sends data in the request body and is required for actions that change state and for sensitive data. File uploads need POST with multipart/form-data encoding.

Form controls and input types

HTML provides a range of form-associated elements, and choosing the correct one gives users the right keyboard, picker and assistive technology behavior without any script.

ControlPurposeNotable behavior
input type text, email, tel, urlSingle-line textMobile keyboards adapt to the type
input type number, date, rangeNumeric and date valuesNative pickers and min, max, step limits
input type checkbox and radioMultiple or single choiceRadio buttons grouped by shared name
select and optionChoice from a listSupports multiple selection and groups
textareaMulti-line textResizable by default
input type fileFile uploadAccepts filters with the accept attribute
buttonSubmit, reset or script actionDefault type is submit inside a form
Common form controls and their purpose

Grouping and labeling are part of the structure. The label element ties visible text to a control, which gives the control its accessible name and enlarges the click target. The fieldset and legend elements group related controls, such as a set of radio buttons, and announce the group to screen readers.

Built-in validation and the constraint validation API

HTML5 introduced declarative constraint validation, which checks values before submission without JavaScript. Browsers block submission and show a message when a constraint fails, and the :invalid and :valid pseudo-classes allow styling.

AttributeApplies toChecks
requiredMost controlsA value is present
minlength, maxlengthText controlsLength of the value
min, max, stepNumber, date, rangeNumeric or date limits
patternText controlsMatch against a regular expression
typeemail, urlBasic format of the value
Validation attributes and what they check

Scripts can extend this with the constraint validation API, including checkValidity(), reportValidity() and setCustomValidity(). The decisive rule is that client-side validation is only a convenience: it can be bypassed, so the server must validate every submission again.

Native forms compared with script-driven submission

Many applications intercept submission and send data with the Fetch API. This can improve the experience, but it should build on a working native form rather than replace it, an approach known as progressive enhancement.

AspectNative HTML formScript-driven submission
Works without JavaScriptYesOnly with a native fallback
Page reloadNavigates to the responseCan update the page in place
Accessibility and autofillBuilt inPreserved only if native controls are kept
Data collectionAutomatic form data setUse the FormData object to mirror it
Native submission versus script-driven submission

A robust pattern keeps the form element with a real action and method, listens for the submit event rather than button clicks, and builds the request from new FormData(form). This preserves Enter-key submission, validation and autofill, and the page still works when the script fails to load or is blocked.

Security and common mistakes

Forms are the main entry point for untrusted input, so many web vulnerabilities begin with them. Sound practice combines correct markup with server-side protection.

  • Trusting client-side validation, which attackers bypass with a direct HTTP request.
  • Missing CSRF protection on POST forms, allowing other sites to trigger actions on behalf of a signed-in user.
  • Sending passwords or personal data with GET, which places them in URLs, history and server logs.
  • Using placeholders instead of labels, which harms accessibility and disappears while typing.
  • Disabling autocomplete for sign-in fields, which weakens password manager support; use proper autocomplete tokens instead.

On the server, every value should be treated as untrusted text of unknown length. Output encoding prevents cross-site scripting, parameterized queries prevent SQL injection, and file uploads need type, size and storage checks independent of the accept attribute.

The short version

An HTML form is the part of a web page where people type or choose information and send it, like a paper form handed over a counter. Boxes for a name, buttons for choices and a send button are all parts of the form.

When the person presses the button, the browser collects everything into a neat package and delivers it to the website’s server. The browser can also check simple mistakes first, such as an empty required box, but the server always checks again.

Where it makes a difference

An online shop rebuilds its checkout form. The previous version used generic text inputs and a script that broke on slow mobile connections. The new form uses type email and type tel inputs with correct autocomplete tokens such as given-name and postal-code, visible labels and a fieldset for the delivery options.

Required fields use the required attribute, the postal code uses a pattern, and the server repeats every check. Submission works as a plain POST request even if the enhancement script fails, while the script adds inline messages when it loads. Browsers now autofill most fields, mobile users get suitable keyboards and testing shows fewer abandoned checkouts caused by validation errors.