HTML Forms and Input Basics
HTML forms collect information and submit it for processing. Use labels, appropriate input types and clear instructions so people can complete forms reliably.
Create a form
<form action="/submit" method="post">
<fieldset>
<legend>Contact details</legend>
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required>
</fieldset>
<button type="submit">Send</button>
</form>
The action identifies where the form submits. The method determines how the browser sends the form data. A fieldset and legend group related controls. The server must be configured to receive and process submissions; validate data on the server even when the browser provides client-side validation.
Form attributes
Common form attributes include action, method, autocomplete and novalidate. Select attributes based on the behavior your form needs.
Input types
Choose a type that matches the information, such as text, email, password, number, date or checkbox. The browser can provide useful controls and basic validation for some types.
Use number for quantities that can be incremented or compared numerically. A phone number or postal code may contain a leading zero, plus sign or formatting characters, so it is text rather than a numeric quantity:
<label for="quantity">Quantity</label>
<input id="quantity" name="quantity" type="number" min="1" step="1">
<label for="phone">Phone number</label>
<input id="phone" name="phone" type="tel" autocomplete="tel">
<label for="postal-code">Postal code</label>
<input id="postal-code" name="postal-code" type="text"
inputmode="numeric" autocomplete="postal-code">
For values such as postal codes, inputmode="numeric" can request a number-focused keyboard on devices without giving up text input semantics.
Input attributes
Attributes such as name, required, placeholder, min, max and maxlength configure inputs. A placeholder is a hint, not a replacement for a visible label.
Input form attributes
An input can use the form attribute to associate it with a form elsewhere in the same document by ID. Use this when the visual layout places controls outside the form element.
Use selects, radio buttons and text areas
Use a select when people choose from a short list, radio buttons when they choose one option from a group, and a textarea for a longer message. Give every control a unique ID and a matching label. Group related radio buttons with a fieldset and legend:
<label for="topic">Topic</label>
<select id="topic" name="topic">
<option value="html">HTML</option>
<option value="css">CSS</option>
</select>
<fieldset>
<legend>Preferred reply method</legend>
<label><input type="radio" name="reply" value="email"> Email</label>
<label><input type="radio" name="reply" value="phone"> Phone</label>
</fieldset>
<label for="message">Message</label>
<textarea id="message" name="message" rows="5"></textarea>
Inside a form, a button defaults to submitting the form. Set type="button" on a button that performs another action, such as opening a help panel.
Choose GET or POST for a form
Use GET when submitting criteria to retrieve or filter information, such as a search. The submitted values appear in the URL, so do not use GET for passwords or other sensitive values. Use POST when sending data that changes or creates something on the server. POST places form data in the request body, but a secure connection and suitable server-side protections are still necessary.
The name attribute supplies a field’s key when the form is submitted. An id connects a control to its label and can identify it for scripts or CSS. A value without a name is generally not included as a named field in the submitted form data.
Use validation to help people fill out a form
Attributes such as required, minlength, max and an appropriate input type let the browser catch common mistakes before submission. These checks improve the form experience, but users can bypass browser-side rules, so validate and safely handle all submitted data on the server too.
Give each control a visible, descriptive label. A placeholder can show an example, but it should not be the only label because it disappears as the person types. For checkboxes and radio buttons, provide text that explains each choice.
For more on connecting labels to controls, see the W3C WAI guide to form labels.
HTML form questions
Does action="/submit" make a form work?
Only if your website has a server route at that address that accepts the selected method and processes the submitted fields. The path in a tutorial example is illustrative; HTML alone does not store or email submissions.
Why does an input need a name?
The name becomes the field key in the form submission. The ID is mainly used to associate a label and target the element; the name is what the receiving code uses to identify the submitted value.
Should a phone number or postal code use type="number"?
No. These values are identifiers, not quantities for arithmetic. Use type="tel" for a phone number and a text input for a postal code so formatting and leading zeroes can be preserved. See MDN's number input guidance for more detail.