The smallest useful form
That’s a whole form. The label tells people what to type, name="drink" is the name the answer is sent
under, and the button sends it. On a real page, pressing Enter in the text box sends the form too: a form with a
submit button does that for free.
The example boxes on this page can’t send forms. Pressing a button inside one does nothing, and the browser doesn’t even run its checks. The order form further down, in What gets sent, is set up differently so you can press Order and see what would go out.
Where the data goes: action and method
HTML can collect answers, but it can’t store them or email them by itself. Something has to receive them: a page on
your own server (written in PHP, Node, Python or similar) or a form service you sign up for. action is
that address. Leave it out and the form sends to the page it’s on.
| method | Where the answers go | Use it for |
|---|---|---|
get (the default) | Into the address, after a ? | Searches and filters, where a link to the results is useful |
post | Inside the request, out of sight of the address bar | Orders, sign-ups, passwords, file uploads: anything that changes something |
With method="get", ordering a flat white takes the browser to an address like this:
https://corner-cafe.example/order?drink=flat+white
The space became a +. With post the same drink=flat+white travels in the
request body instead. POST isn’t secret on its own, though: it’s the https:// that keeps the data
private on the way. You may also see action="mailto:..." in old tutorials. It only opens the
visitor’s email app, if they have one set up, so it isn’t a dependable way to get messages.
Labels: for and id, or wrap it
Every field needs a label. There are two ways to tie one to its field, and both work the same:
The first label points at the box with for="name", which matches id="name". The second
wraps the checkbox, so it needs no ids at all. Click the words “Takeaway cup” and the box ticks. That bigger click
area helps everyone, and screen readers read the label out when the field gets focus.
Input types
One <input> tag covers most fields. The type changes what the browser shows, which
keyboard phones open, and what it checks:
| type | What you get | Good to know |
|---|---|---|
text | A plain one-line box (the default) | No checks |
email | A keyboard with @ on phones | Needs an @ with text on both sides |
tel | A number pad on phones | No format check: phone numbers differ too much |
number | Up and down arrows | Checks min, max and step |
date | A date picker | Always sends 2026-12-05 style, whatever it shows |
password | Dots instead of letters | Only hides it on screen: send it over https |
url | A keyboard for web addresses on phones | Must start with a scheme like https:// |
file | A “Choose file” button | accept filters the file picker |
hidden | Nothing on screen | Sent with the rest, like an order id |
Checkboxes and radio buttons are inputs too, and so is the slider, type="range", which has its own
slider tutorial. To style a text box quickly, the
Text Box Generator writes the CSS for you.
Checkboxes and radio buttons
Radio buttons that share a name form one group, and only one in the group can be picked. Checkboxes
are separate yes or no switches:
- Give the radios the same name, different values. Change one of the sizes to
name="size2"in the box and you can pick two sizes at once. It’s an easy slip when you copy and paste a radio. -
checkedpicks the starting choice. With radios it’s kind to start on one, so the question can’t be left blank by accident. -
A ticked checkbox sends its
value. Leave the value out and it sends the wordon. An unticked checkbox sends nothing at all, not even its name.
Textarea for longer answers
<input> is one line only. For a note or a message, use <textarea>:
rows="4" sets the starting height in lines. The starting text goes between the tags, not in a
value attribute. resize: vertical lets people drag it taller but not wider, so it can’t
break your layout, and maxlength stops typing at 300 characters. One trap: anything between the tags
counts as text, spaces included. If you write <textarea> and indent the closing tag on the next
line, the box starts with a few spaces in it and its placeholder never shows.
Drop-down lists with select
A <select> holds <option>s. <optgroup> puts a heading over a
few of them:
-
The
valueis what gets sent (flat-white), the text between the tags is what people see (Flat white). Without avalue, the text is sent. -
selectedpicks the starting option. Remove it from Flat white and the list starts on “Choose a drink”. - The placeholder option. A first option with
value=""plusrequiredon the select means “Choose a drink” doesn’t count as an answer, so the browser asks for a real choice.
Add multiple and people can pick more than one. size sets how many rows show:
Multi-selects are awkward with a mouse and worse on a phone, so checkboxes are often the friendlier choice. The Dropdown Generator builds either one from a plain list.
Placeholder is not a label
A placeholder is the grey hint inside an empty box. It’s tempting to use it instead of a label to
save space. Type into both boxes here:
The second box forgets what it was for the moment you type, so anyone checking their answers later has to guess.
Light grey hint text is also hard to read for a lot of people. Use the placeholder for an example of the format, like
[email protected], and keep a real label above it.
Required fields and the browser’s checks
Browsers check a few things before a form is sent, with no JavaScript needed:
| Attribute | Example | What Chrome says (shortened) |
|---|---|---|
required | <input name="name" required> | Please fill out this field. |
type="email" | typing sam.cafe | Please include an '@' in the email address. |
min / max | max="6", typing 9 | Value must be less than or equal to 6. |
minlength | minlength="2", typing “S” | Please lengthen this text to 2 characters or more. |
pattern | pattern="[0-9]{5}" | Please match the requested format. |
The wording changes from browser to browser and follows the visitor’s language. The messages show up as a small
bubble when someone presses the submit button, and the form isn’t sent until every field passes. You can also
color the fields that fail. Type sam.cafe in the email box below, then click somewhere else:
-
:user-invalidonly turns red after the person has changed the field and moved on, so nobody gets shouted at on page load.:invalidmatches straight away: the cups box starts at 9, so it would be red before anyone touches it. -
patternhas to match the whole value, not a part of it, so123456fails a five-digit pattern. Add atitleor a line of help text that says what you expect, because “the requested format” tells people nothing. -
type="tel"has no check at all, even for letters. Add apatternif you need one. - Check again on the server. Anyone can turn these checks off in their browser, or send data
without your form, so they help honest visitors and nothing more. To turn them off yourself, put
novalidateon the form.
Group fields with fieldset and legend
<fieldset> draws a box around fields that belong together, and <legend> is the
title on its border:
Screen readers announce the legend as people move into the group, so “Small, radio button” comes with the
question it answers. That’s why a group of radio buttons should nearly always sit in a fieldset. disabled on a fieldset
switches off every field inside it at once, and disabled fields aren’t sent.
What gets sent when you press the button
When a form is sent, the browser collects a name=value pair from every field that has a name and
joins them with &. Here’s the coffee order form put together. Fill it in and press Order; leave the
name empty first to see the browser’s own message:
<form action="/order" method="get">
<p>
<label for="o-name">Your name</label><br>
<input type="text" id="o-name" name="name" autocomplete="name" required>
</p>
<p>
<label for="o-drink">Drink</label><br>
<select id="o-drink" name="drink" required>
<option value="">Choose a drink</option>
<option value="latte">Latte</option>
<option value="flat-white">Flat white</option>
<option value="mocha">Mocha</option>
</select>
</p>
<fieldset>
<legend>Size</legend>
<label><input type="radio" name="size" value="small" checked> Small</label>
<label><input type="radio" name="size" value="large"> Large</label>
</fieldset>
<p>
<label><input type="checkbox" name="extra-shot" value="yes"> Extra shot</label>
</p>
<p>
<label for="o-note">Anything else?</label><br>
<textarea id="o-note" name="note" rows="2"></textarea>
</p>
<button type="submit">Order</button>
</form> To show the result, this one box adds a few lines of JavaScript after the form that stop the real send and print the pairs. The code card shows only the form, which is what you’d copy.
Ordering a large latte with an extra shot, as Sam, sends this:
/order?name=Sam&drink=latte&size=large&extra-shot=yes¬e= - An empty text box or textarea is still sent, with an empty value (
note=). - Only the picked radio button is sent. An unticked checkbox isn’t sent at all.
-
Fields without a
nameare skipped, and so aredisabledones.readonlyfields can’t be edited but are still sent. -
A file input sends only the file’s name unless the form has
method="post"andenctype="multipart/form-data". Then the file itself goes along.
Help the browser fill it in
Browsers remember names, emails, addresses and passwords. The autocomplete attribute tells them which
is which, so they offer the right one:
<label for="a-name">Name</label>
<input id="a-name" name="name" autocomplete="name">
<label for="a-email">Email</label>
<input type="email" id="a-email" name="email" autocomplete="email">
<label for="a-phone">Phone</label>
<input type="tel" id="a-phone" name="phone" autocomplete="tel">
<label for="a-pw">New password</label>
<input type="password" id="a-pw" name="password" autocomplete="new-password">
Other useful values are street-address, postal-code, current-password for
log-in forms and one-time-code for codes sent by text message. autocomplete="off" asks the
browser not to suggest anything, but password managers often ignore it on log-in fields, so don’t count on it.
Common mistakes
- No
name. The field looks fine and works on screen, but it’s never sent:Only the first one is sent <!-- Sent: ?drink=latte --> <input name="drink" value="latte"> <!-- Not sent at all: no name --> <input id="size" value="large"> - A
<button>that sends the form by surprise. Inside a form, a button with notypeis a submit button. A “Show menu” button will send the order. Writetype="button"on buttons that do something else; the button tutorial covers the details. - A label’s
forthat doesn’t match.for="email"withid="e-mail"connects nothing. Ids must also be unique on the page. - Using
disabledwhen you meantreadonly. Disabled fields aren’t sent. - Trusting the browser’s checks. They’re for the visitor’s comfort. Your server has to check the data again.
When you’d rather not type all this by hand, the HTML Form Builder puts a form together from a list of fields, with labels and checks already in place.
Questions people ask
How do I make a form in HTML?
Put your fields inside <form action="/order" method="post">, give each one a <label> and a name, and finish with <button type="submit">Order</button>. For example: <label for="drink">Drink</label> <input type="text" id="drink" name="drink">. The action is the address that receives the answers, and method is get or post.
Where does the data go when an HTML form is submitted?
To the address in the action attribute, or to the same page if there is no action. HTML can't save or email the answers on its own, so that address has to be something that reads them: a script on your own server or a form service. With method="get" the answers are added to the address after a ?, and with method="post" they travel in the request body.
What is the difference between GET and POST in a form?
GET puts the answers in the address, like /search?q=latte, so the result can be bookmarked or shared. Use it for searches and filters. POST sends them in the request body, so use it for anything that changes something, like an order or a sign-up, and for passwords and file uploads. POST alone doesn't encrypt anything; https does.
How do I connect a label to an input?
Either give the label a for attribute that matches the input's id, <label for="email">Email</label> <input id="email" name="email">, or put the input inside the label: <label><input type="checkbox" name="takeaway"> Takeaway cup</label>. Both make the words clickable and let screen readers read the label when the field gets focus.
How do I make a field required in HTML?
Add the required attribute: <input name="name" required>. The browser won't send the form while the field is empty and shows a message such as "Please fill out this field." It works on text boxes, textareas, selects, checkboxes and radio groups. Check the data on your server as well, because these checks can be switched off.
How do I add a placeholder option to a select?
Make the first option empty and the select required: <select name="drink" required><option value="">Choose a drink</option><option value="latte">Latte</option></select>. The browser treats "Choose a drink" as no answer and asks for a real choice when the form is sent.
What is the difference between input and textarea?
An <input> is one line, good for a name or an email. A <textarea> takes several lines, good for a note or a message, and its starting text goes between the tags: <textarea name="note" rows="4">Please write "Sam" on the cup.</textarea>. Set its height with rows and use resize: vertical in CSS to stop people dragging it wider.
Why is my form field not being sent?
Most often it has no name attribute: only fields with a name are sent. Other causes: the field is disabled (use readonly if it should be sent but not edited), it's an unticked checkbox (those send nothing), it sits outside the <form> tags, or it's a file input in a form without method="post" and enctype="multipart/form-data", which sends only the file name.
Why does my button submit the form when I click it?
A <button> inside a form is a submit button unless you say otherwise. Add type="button" to buttons that should only run your own JavaScript, like <button type="button">Show menu</button>, and keep type="submit" for the one that sends the form.