Skip to the content

Lesson 10 of 296

Forms and inputs

What you will learn

  • How a form sends data to a server
  • The <input> element and its many types
  • Labels and why every field needs one
  • Text areas, drop-down lists, checkboxes and radio buttons
  • Buttons and the most common form bug
On this page

What a form is

A form collects information from the user and sends it to a server. Login pages, sign-up pages, search boxes, checkout pages and contact pages are all forms.

<form action="/subscribe" method="POST">
  <label for="email">Email address</label>
  <input type="email" id="email" name="email">
  <button type="submit">Subscribe</button>
</form>
PartMeaning
actionThe URL that receives the data
methodHow the data is sent: GET or POST
nameThe key for this field's value in the data sent to the server

When the user clicks Subscribe, the browser sends email=sam@example.com to /subscribe.

A field without a name is not sent. This is a very common bug.

GET or POST?

MethodHow data is sentUse for
GETAdded to the URL: /search?q=shoesSearching and filtering. Safe to bookmark and share.
POSTInside the body of the request, not visible in the URLAnything that creates or changes data: sign-up, login, payment, orders

Never send passwords with GET. They would appear in the address bar, the browser history and server logs.

Labels

Every field needs a label that says what to type. Connect the label to the field: the label's for must match the input's id.

<label for="first-name">First name</label>
<input type="text" id="first-name" name="firstName">

Why this matters:

  • Screen readers read the label when the user reaches the field.
  • Clicking the label puts the cursor in the field. This gives a bigger area to tap on phones.

A placeholder is not a label. Placeholder text disappears when the user starts typing, so they forget what the field was for. It is also often too pale to read.

<input type="text" placeholder="e.g. Sam">     only a hint, not a label

Input types

The type attribute changes how a field works. On phones, the right type also shows the right keyboard.

TypeUse forExtra behaviour
textShort text-
emailEmail addressesChecks for an @ sign; shows @ on phone keyboards
passwordPasswordsHides the characters
numberAmountsOnly numbers; up and down arrows
telPhone numbersShows a number pad on phones
urlWeb addressesChecks the format
dateDatesShows a date picker
timeTimesShows a time picker
searchSearch boxesMay show a clear (x) button
checkboxYes/no, or choose many-
radioChoose exactly one from a group-
fileUploading filesOpens the file picker
hiddenData the user does not seeNot shown at all
rangeA slider-

Tip: use type="tel" or type="text" for phone numbers and card numbers, not type="number". They are not amounts: a phone number can start with 0 and should not have up and down arrows.

Textarea: long text

<label for="message">Message</label>
<textarea id="message" name="message" rows="5"></textarea>

Select: a drop-down list

<label for="country">Country</label>
<select id="country" name="country">
  <option value="">Please choose</option>
  <option value="uk">United Kingdom</option>
  <option value="ie">Ireland</option>
</select>

The value is what is sent to the server. The text between the tags is what the user sees.

Checkboxes: choose any number

<input type="checkbox" id="news" name="newsletter" value="yes">
<label for="news">Send me the newsletter</label>

If the box is not ticked, nothing is sent for that field.

Radio buttons: choose exactly one

All radio buttons in one group must share the same name. That is what links them.

<fieldset>
  <legend>Delivery</legend>
  <input type="radio" id="std" name="delivery" value="standard" checked>
  <label for="std">Standard</label>
  <input type="radio" id="exp" name="delivery" value="express">
  <label for="exp">Express</label>
</fieldset>

<fieldset> groups related fields. <legend> is the title of the group. Screen readers say "Delivery, Standard" so the user knows what the choice is about. checked selects an option by default.

Buttons and the common bug

<button type="submit">Send</button>     sends the form
<button type="button">Show more</button> does nothing by itself; used with JavaScript
<button type="reset">Clear</button>     empties every field (rarely a good idea)

The bug: a <button> inside a form with no type acts as submit. So a "Show password" button with no type will send the form by accident. Always write the type.

Other useful attributes

AttributeWhat it does
valueThe starting value
placeholderA hint shown when the field is empty
disabledThe field cannot be used and is not sent
readonlyThe field cannot be changed but is sent
autocompleteHelps the browser fill the field, for example autocomplete="email" or "current-password"
autofocusPuts the cursor in this field when the page loads

A complete example

<form action="/register" method="POST">
  <label for="name">Full name</label>
  <input type="text" id="name" name="name" autocomplete="name">

  <label for="reg-email">Email</label>
  <input type="email" id="reg-email" name="email" autocomplete="email">

  <label for="pw">Password</label>
  <input type="password" id="pw" name="password" autocomplete="new-password">

  <input type="checkbox" id="terms" name="terms" value="accepted">
  <label for="terms">I accept the terms</label>

  <button type="submit">Create account</button>
</form>

Common mistakes

  • Missing name, so the value is never sent.
  • Using placeholder instead of a label.
  • A button with no type inside a form.
  • Radio buttons with different names, so the user can tick all of them.
  • type="number" for phone numbers.
  • Sending passwords with GET.

How to say it in an interview

"A form sends each field's name and value to the URL in action, using GET for searches and POST for anything that changes data. Every field has a label connected with for and id, because a placeholder is not a label. I choose the right input type so phones show the right keyboard. Radio buttons in a group share a name. And I always set the type on buttons, because a button in a form defaults to submit."

Key words

WordMeaning
FormA group of fields that sends data to a server
actionWhere the form data is sent
methodHow it is sent: GET or POST
nameThe key for a field's value
LabelThe visible title of a field
PlaceholderA light hint inside an empty field
Fieldset / legendA group of related fields and its title

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.