Loading...

HTML essentialsContent

Forms & inputs

7 min lesson

Next
  • A form collects information from the user and sends it to a server for processing.
  • A form is wrapped in <form>, and each field is an <input> or another control with a name attribute.
  • Think of a form as a paper questionnaire: each field is a question, and the name is the label the server uses to read the answer.
  • Every input needs a <label> tied to it by id and for, so users know what to type and screen readers can announce it.
  • Common input types are text, email, password, number, checkbox and radio.
  • This builds on Tables & data: both send structured data, but a form lets the user provide it.
  • The example has a name field, an email field, a checkbox and a submit button; you will see a small signup form.
  • Next, Select, datalist, textarea & fieldset adds more control types.
Keep in mind
  • Every input needs a name so the server receives it.
  • Every input needs a label tied by id and for.
  • Use the right type (email, number) to get free browser checks.
  • Never use placeholder as a replacement for a label.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Forms</title>
</head>
<body>
  <!-- The form wraps all its fields -->
  <form action="/signup" method="post">
    <!-- A label tied to the input by id=name -->
    <label for="name">Name</label>
    <input type="text" id="name" name="name" required>
    <br>
    <!-- type=email gives a basic check on submit -->
    <label for="email">Email</label>
    <input type="email" id="email" name="email" required>
    <br>
    <!-- A checkbox for yes/no choices -->
    <input type="checkbox" id="updates" name="updates" value="yes">
    <label for="updates">Send me updates</label>
    <br>
    <!-- The button submits the form -->
    <button type="submit">Sign up</button>
  </form>
</body>
</html>

What happens: A small form with a "Name" field, an "Email" field, a "Send me updates" checkbox and a "Sign up" button.

Breaking it down

form

<form action="/signup" method="post">

Groups all fields and tells the browser where to send them (action) and how (method).

label

<label for="name">Name</label>

Shows the field's purpose. Clicking the label focuses the input when for matches the input's id.

input text

<input type="text" id="name" name="name">

A single-line text field. The name is the key the server uses to read the value.

input email

<input type="email" ...>

A text field that the browser checks for an @ and a domain before submitting.

checkbox

<input type="checkbox" ... value="yes">

A yes/no toggle. The value is sent only when checked.

submit button

<button type="submit">Sign up</button>

Sends the form to the server. Without a submit control, the user cannot send it.

Common input types

TypeWhat it collects
textFree text on one line
emailAn email address
passwordHidden characters
numberA numeric value
checkboxYes or no choice
radioOne choice from a group

A helpful analogy

A form is like a paper form at a doctor's office: each line is a question with a blank, the label tells you what to write, and the receptionist (the server) reads the answers using the field names.

Nesting rules

  • 1.Every input must live inside a form to be submitted.
  • 2.Each id must be unique, because labels, scripts and links use it to find the field.

Common mistakes

Using placeholder as the only label

Avoid

<input type="text" placeholder="Name">

Use instead

<label for="name">Name</label><input id="name" type="text">

Placeholders vanish as soon as the user types, so the field's purpose is lost. Screen readers may also skip them.

Forgetting the name attribute

Avoid

<input type="text" id="name">

Use instead

<input type="text" id="name" name="name">

Without name, the value is not sent to the server. The field looks fine but the data is lost.

Quick tips

  • •Tab through the form with the keyboard; every field should receive focus in a sensible order.
  • •Use required only for fields the server truly needs; too many required fields annoy users.
Previous
Next