HTML Forms and Inputs
Level 2 · HTML
How a form collects what people type and sends it to a server. The input types, labels and checks that make a form easy to fill in.
Course contentsLesson 6 of 14
Level 1 · How a website works
Level 3 · CSS basics
Level 4 · Page layout
Level 5 · JavaScript in the page
After this lesson you can
- Build a form with labels and inputs
- Pick the right input type for each field
- Choose GET or POST for a form
12 mincovers 3 glossary entries
Worth reading first: HTML Elements and Attributes
How a form collects data and sends it
An HTML form is the part of a page where people type or pick things to send. It is a <form> element holding fields and a button. The <input> element alone has 22 types, from text and email to date and checkbox. When the button is pressed, the browser sends every field to the server.
Two attributes on the form decide where the data goes. The action attribute names the address that receives it. The method attribute says how it travels. GET puts the fields in the address itself, after a question mark. POST sends them inside the request, out of sight. GET is the default.
Use GET for a search, where the address can be saved and shared. Use POST for anything that changes data, such as an order or a sign-up. A password should never go by GET. The address is kept in the browser's history and in the server's logs.
Each field needs a name attribute, or it is not sent at all. It also needs a label. A <label> with a for attribute that matches the field's id joins the two. Clicking the label then puts the cursor in the box, and a screen reader reads the label out.
The type attribute does a lot of work. On a phone, type="email" brings up a keyboard with an @ key, and type="tel" brings up the number pad. A date field opens a calendar. The browser also checks the shape: an email field will not send without an @ sign.
Add required and the form will not send while that field is empty. Add minlength="8" to a password and anything shorter is turned back. These checks show a message at once, before anything leaves the browser, which saves the user a wait and a wasted trip.
People often treat those checks as the real ones. They are only for comfort. Anyone can skip them by changing the page or sending a request by hand. The server must check every field again before it saves anything.
<form action="/signup" method="post">
<label for="email">Email</label>
<input id="email" name="email" type="email" required>
<label for="pass">Password</label>
<input id="pass" name="pass" type="password" minlength="8" required>
<button type="submit">Sign up</button>
</form>Worked example
The words for this lesson3
Check yourself
Which form method should a login page use?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.