HTML Forms, Labels, and Inputs
HTML Living Standard
A form collects data from controls. The browser can validate those controls, turn their selected values into name–value pairs, and send them to a URL.
On this page
What does <form> do?
A form groups controls used for answering a question or sending information. An input is a control the reader can type into. A submit button asks the browser to send the form. The name on an input determines the name used in the submitted data.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>First form</title>
</head>
<body>
<form method="get">
<label for="product">Product</label>
<input id="product" name="product">
<button type="submit">Submit</button>
</form>
</body>
</html>
What to expect Type pen and submit. ?product=pen appears in the browser address. This sample reloads the page with that query in its URL.
Connect a label to a field
Match the label’s for to the input’s id. The id identifies the element in the document. The name is the key used when the form sends the field. A visible label gives readers a stable cue even after they start typing.
How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.
<label for="customer-email">Email address</label>
<input id="customer-email" name="email" type="email" autocomplete="email">
What to expect Click the label to move focus to the input. An email typed into this control will use email as its submission key.
Choose input types by data
The type can change the browser’s control, validation, or on-screen keyboard. Use email for email addresses, number for quantities, and date for calendar dates. An email field checks the syntax of a value. It cannot verify that the address belongs to the user.
How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.
<form>
<label for="email">Email</label>
<input id="email" name="email" type="email" required>
<label for="quantity">Quantity</label>
<input id="quantity" name="quantity" type="number" min="1" value="1">
<label for="pickup">Pickup date</label>
<input id="pickup" name="pickup" type="date">
<button type="submit">Continue</button>
</form>
What to expect Try entering pen in Email and submitting. The browser asks for a valid email address. The date field can offer a calendar picker, and its submitted value uses YYYY-MM-DD.
Send form data with GET or POST
The action identifies the request destination. GET adds the submitted values to the URL query string and fits actions such as search. POST puts the submitted form data in the request body and is commonly used for changes such as placing an order. A form sends values from named, eligible controls. A server route handles the request.
How to try it In index.html, replace the content between <body> and </body> with this example. Save the file and reload it in the browser.
<form action="/orders" method="post">
<label for="order-email">Email address</label>
<input id="order-email" name="email" type="email" required>
<button type="submit">Place order</button>
</form>
What to expect On a server that handles POST /orders, the browser sends a request whose form body includes email. In a standalone HTML file you can still test whether the browser blocks invalid input.