A form is how a page sends typed values back to a server. The markup is simple, but two details decide whether it works. Every control needs a name attribute, and the choice of input type decides which keyboard the visitor gets, which validation applies, and what built-in behaviour the browser provides.
This is the first lesson in the HTML Forms module. The next lesson in this module is about "Why Do Labels Matter for Every Form Field?".
What does the form element do?
The form element wraps the controls, names the destination, and says how the data should travel. Those three jobs all live in one tag, and each one has its own attribute.
<form action="/subscribe" method="post">
<label for="email">Email address</label>
<input type="email" id="email" name="email" required>
<button type="submit">Subscribe</button>
</form>The action attribute holds the URL that receives the data. The method attribute says how the data travels to that URL.
A post request puts the values in the body of the request, so use it for anything sensitive. A get request appends the values to the URL, which is handy for a search form and completely wrong for a password.
If you leave out the method attribute, the browser falls back to get. That is why a form that carries sensitive data has to ask for post itself.
Why does every control need a name?
Because the name is the label that the server sees. When the page sends its values, the server receives a list of names and the values that belong to them.
When you submit the form above, the body of the request contains the value of the control that is called email. The name is what makes that pairing possible.
A control with no name is never sent at all. The browser collects it, the visitor fills it in, and then it vanishes on the way to the server.
POST /subscribe
[email protected]The id attribute does a different job. It is the internal identifier that a label points at, and the one that CSS and JavaScript use to find the control.
The two attributes are independent, and beginners often mix them up. Every control needs a name so that it can be submitted. It needs an id only when something else has to refer to it.
Which input types exist?
The type attribute is the most valuable attribute on an input, because the browser uses it to give the visitor the right control and the right keyboard.
<input type="text" name="name">
<input type="email" name="email">
<input type="password" name="password">
<input type="number" name="quantity" min="1" max="99" step="1">
<input type="date" name="start-date">
<input type="time" name="reminder">
<input type="url" name="website">
<input type="tel" name="phone">
<input type="search" name="q">
<input type="color" name="shade">
<input type="file" name="upload" accept="image/*">
<input type="checkbox" name="topics" value="java">
<input type="radio" name="plan" value="free">The email, url, and tel types give the right keyboard on a phone, and they give a basic format check on a desktop.
The number type gives a numeric keypad. The min, max, and step attributes constrain the value that the browser will accept.
The date and time types give a picker, and that removes the whole date-formatting problem in one step.
The color type gives a swatch picker, and it returns a hex value.
The file type opens the system file dialog, and the accept attribute filters the types that are shown.
Two rules keep the behaviour predictable, and they are worth following on every form you build.
Use the type="text" as the fallback, and never leave the attribute off. The default type behaves differently from what a visitor expects when they tab into the field.
If you add a pattern constraint, always pair it with a visible example. A pattern that the visitor cannot see is a form that they cannot complete.
What about longer text and several choices?
Two more controls cover the remaining cases. A textarea takes multi-line text, and a select takes one choice from a list.
Both of them send their value with the name of the control, exactly like an input does.
<label for="message">Your message</label>
<textarea id="message" name="message" rows="5" cols="40" maxlength="500"></textarea>
<label for="country">Country</label>
<select id="country" name="country">
<option value="" selected disabled>Choose one</option>
<option value="uk">United Kingdom</option>
<option value="ie">Ireland</option>
</select>Notice the selected, disabled option in the select. It is the placeholder that shows before the visitor chooses anything. Disabling it stops it from being a valid submission, and that is how you make the "choose one" instruction mean something.
Why does the autocomplete matter so much?
Typing the same details into a dozen forms is the most irritating part of being online, and the autocomplete attribute fixes it.
The browser stores what the visitor entered before and offers it again. The name, the email address, the card number, the address. The form arrives pre-filled on the next site.
<input type="text" name="name" autocomplete="name">
<input type="email" name="email" autocomplete="email">
<input type="tel" name="phone" autocomplete="tel">
<input type="text" name="street" autocomplete="street-address">
<input type="text" name="postcode" autocomplete="postal-code">
<input type="password" name="password" autocomplete="new-password">The token names are defined by the browser, and using the right one is what makes the suggestion appear.
A field with a made-up token gets no help at all, so it is worth learning the handful you will really use. There is name, email, tel, username, new-password, current-password, street-address, postal-code, country, and one-time-code.
Use the autocomplete="off" only when a field genuinely should not be remembered, and never on a login form. Turning it off there breaks password managers, and visitors end up typing their passwords by hand.
What are the hidden fields for?
A hidden input carries a value to the server without showing it to the visitor. It sits in the markup like any other control, but it has no visible part on the page.
It is the standard way to send state that the server needs but the visitor never fills in. That might be which form was submitted, a campaign identifier, or a value that the browser computed.
<input type="hidden" name="form_id" value="newsletter-2026">
<input type="hidden" name="referrer" value="pricing-page">Because the visitor can edit a hidden field, you have to treat its contents as untrusted.
It is never a place for a secret, a password, or anything you would be upset to see tampered with. The server has to validate it like any other value that arrives.
Quick reference
- email, url, tel give the right keyboard on a phone, and a basic format check on a desktop.
- number gives a numeric keypad, and the min, max, and step attributes constrain the value.
- date and time give a picker, which removes the date-formatting problem in one step.
- color gives a swatch picker and it returns a hex value.
- file opens the system file dialog, and the accept attribute filters the types that are shown.
- The form needs an action and a method, and you use post for anything sensitive.
- Every control needs a name to be submitted, and the id is for labels, CSS, and JavaScript.
- You choose the input type that matches the data, and then the mobile keyboard and the validation follow.
- The
type="text"is the fallback, and you never omit the type attribute. - A disabled placeholder option stops an empty select from being submitted as a real answer.
Try it yourself: you build a signup form with a name, an email, a quantity, a start date, and a choice of plan. You submit it to a local server and read the body of the request in the server log. Then you remove the name from one of the fields, you submit it again, and you watch that value disappear from the request.