Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
HTML  ›  Capstone Project

Forms — Letting Visitors Talk Back

HTML 15 minA form sends each named field to its action URL using its method, and every input needs a label tied by for and id so people know what it is, plus a name so the server receives it.

Forms — Letting Visitors Talk Back

The bakery wants customers to pre-order a cake: name, phone number, pickup date and a message for the icing. That is a form. Forms are where a page stops only talking and starts listening, and a few small attributes decide whether the data actually arrives and whether everyone can fill it in.

The rule

Every input needs a label for people and a name for the server.

<form action="/orders" method="post">
  <label for="customer">Your name</label>
  <input id="customer" name="customer" type="text" required>

  <label for="phone">Phone</label>
  <input id="phone" name="phone" type="tel" required>

  <label for="pickup">Pickup date</label>
  <input id="pickup" name="pickup" type="date">

  <label for="icing">Message on the cake</label>
  <textarea id="icing" name="icing" maxlength="40"></textarea>

  <button type="submit">Place order</button>
</form>

The form. action is where the data goes; method is how. post sends the data in the request body, which suits creating an order. get puts the data in the URL, which suits searches you might bookmark.

The label (for people). A <label> whose for matches an input's id is tied to that input. Screen readers announce the label when the field gets focus, and clicking the label puts the cursor in the field, which makes small targets easier to hit. Placeholder text is not a label: it disappears as soon as someone types.

The name (for the server). When the form is submitted, the browser sends each field as name=value, for example customer=Asha&phone=98450.... A field with no name is simply not sent, even if the visitor filled it in.

The type. type="email", tel, number, date and others give the right phone keyboard and some built-in checking. required, min, max and maxlength add simple checks before anything is sent. These help visitors, but the server must still check everything, because anyone can bypass the browser.

The button. A <button> inside a form submits it by default. Write type="submit" to be clear, and type="button" for buttons that should not submit.

The shortcut

Check each field with the "two readers" test: Can a person tell what it is for? (a visible label tied by for and id) and Can the server tell what it is? (a name). One missing reader means a broken field.

Checking the icing message fieldStep 1 of 4
  1. Reader one: people

    There is a <label for="icing">, and the textarea has id="icing". They match, so the label is tied to it.

The silent trap is a field with an id but no name. It looks fine, the label works, and the value never reaches the server.

Drills

Answer fast — trust the rule.

quick drill

A field has id="email" and a matching label, but no name. What happens on submit?

quick drill

How do you tie <label>Phone</label> to <input id="phone">?

quick drill

Which method suits a search form whose results people may want to bookmark?

quick drill

The cake form uses required and maxlength. Does the server still need to check the data?

Remember

  • Every input needs a label for people and a name for the server.
  • Two readers test: label tied by for and id, plus a name.
  • Trap: a field with no name is silently left out; browser checks never replace server checks.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainA form that works for everyone

Describe the markup for the bakery's cake pre-order form (name, phone, pickup date, icing message). Explain what action, method, label/for/id and name each do, and why the server must still check the data.