We’re moving to a new home! Our website is currently in test mode while we update and transfer our content. Some pages and resources may be temporarily unavailable. We’ll be back with all resources shortly. Thank you for your patience.

HTML & CSS | 02.2 Forms, Labels, Inputs and Buttons

Lesson objective

Create clearly labelled forms using suitable controls. Explain how id, name and value work together, group related choices and understand what happens when a form is submitted.

Learn

02.2 | FORMS, LABELS, INPUTS AND BUTTONS

01 | A CONVERSATION WITH THE USER

A robotics club needs to know who wants to join, which workshop they prefer and what they hope to build. A form turns those questions into controls people can use.

HTML provides the structure and native controls. A working application also needs somewhere to process the submitted data. A form alone does not save information to a database or send an email.

<form action="/club-signup" method="post">
  <p>
    <label for="student-name">Student name</label>
    <input type="text" id="student-name" name="student_name">
  </p>
  <p>
    <label for="student-email">Email address</label>
    <input type="email" id="student-email" name="email">
  </p>
  <button type="submit">Send application</button>
</form>

Here, action names the destination and method specifies how data is sent. /club-signup is an example endpoint that your server would need to implement. It is not a working service provided by this lesson.

02 | LABELS, IDS AND NAMES

<label for="student-name">Student name</label>
<input type="text" id="student-name" name="student_name">

These attributes have different jobs:

  • for on the label matches the control’s id. This associates the label with its input. Clicking the label normally focuses the input.
  • id identifies this element in the page. Each id must be unique.
  • name supplies the field’s key when its data is submitted.

If Alex enters a name, the submitted entry could be student_name=Alex. The id does not replace name in submitted data. A control without name is usually omitted from the form’s entries.

A placeholder is a temporary hint inside a field, not a replacement for a visible label. It disappears as the user types.

03 | CHOOSE A SUITABLE INPUT TYPE

<label for="contact-email">Email address</label>
<input type="email" id="contact-email" name="email">

<label for="team-size">Team size</label>
<input type="number" id="team-size" name="team_size"
       min="1" max="6" step="1">

<label for="meeting-date">Preferred meeting date</label>
<input type="date" id="meeting-date" name="meeting_date">

type="text" suits a short line of text. email can provide email-oriented input behaviour and basic format checking. number suits quantities, while date provides a date control whose appearance varies by browser.

Use text for identifiers such as a postcode or student code. They are not quantities to calculate with and may contain letters or leading zeros.

min, max and step describe allowed numeric values. required can make a supported control compulsory. The next lesson explores validation in detail. Browser checks do not verify that an email address exists and do not replace server-side validation.

04 | ONE CHOICE OR SEVERAL?

<fieldset>
  <legend>Choose one workshop</legend>
  <label>
    <input type="radio" name="workshop" value="build">
    Robot building
  </label>
  <label>
    <input type="radio" name="workshop" value="code">
    Python coding
  </label>
</fieldset>

<fieldset>
  <legend>Interests: choose any that apply</legend>
  <label>
    <input type="checkbox" name="interest" value="sensors">
    Sensors
  </label>
  <label>
    <input type="checkbox" name="interest" value="design">
    3D design
  </label>
</fieldset>

Radio buttons in the same group share a name, so only one can be selected. Give each choice a different value. Checkboxes support independent selections, so the user can choose several interests or none.

A label can wrap its input, as in these examples, without using for. If you add ids, keep them unique even when the names are shared.

fieldset groups related controls, and legend names the group. Only checked radio buttons and checkboxes contribute entries. If both interests are checked, interest appears twice with different values. The server must handle those repeated entries appropriately.

05 | MENUS AND LONGER RESPONSES

<label for="experience">Coding experience</label>
<select id="experience" name="experience">
  <option value="">Choose an option</option>
  <option value="new">I am new to coding</option>
  <option value="some">I have tried coding</option>
  <option value="confident">I code regularly</option>
</select>

<label for="project-idea">What would you like to build?</label>
<textarea id="project-idea" name="project_idea" rows="4"></textarea>

A select offers a set of options. The visible wording can be friendly while value supplies a short code for processing. “I am new to coding” submits new in this example.

A textarea supports multiple lines and needs a closing tag. Initial text goes between its opening and closing tags. By contrast, an input is a void element and does not have a closing tag.

Keep instructions short and labels descriptive. Choose a control that matches the kind of answer you need.

06 | BUTTONS AND SUBMISSION

<button type="submit">Send application</button>
<button type="button">Show workshop details</button>

A submit button starts form submission, subject to the browser’s validation checks. A button with type="button" has no submission behaviour by default; an action such as revealing details needs programming.

A button inside a form commonly defaults to submit when type is omitted. Write the type explicitly to avoid accidental submissions.

type="reset" restores controls to their initial values, which are not necessarily empty. Reset buttons can discard work accidentally, so include them only when they serve a clear purpose.

Do not nest a form inside another form. Put separate forms alongside one another.

07 | GET AND POST: WHERE DOES THE DATA GO?

With GET, form data is normally added to the destination URL as a query string. This suits many searches and filters. With POST, data is sent in the request body. This is commonly used for actions that create or change data.

<form action="/search" method="get">
  <label for="search-topic">Search lessons</label>
  <input type="search" id="search-topic" name="q">
  <button type="submit">Search</button>
</form>
<!-- A search for binary can produce /search?q=binary -->

POST is not encryption. HTTPS protects data in transit. Servers must still validate input and apply appropriate security controls. A password input masks characters on screen but does not make the whole application secure.

Disabled controls and unnamed controls are generally omitted. Unchecked checkboxes are omitted too. The form’s submitted entries are not simply a copy of every visible thing on the page.

08 | TRY IT: INSPECT WHAT WOULD BE SENT

Use fictional details. This demonstration stays in this page: JavaScript prevents submission and shows the form entries below. It does not send or save them.

Choose one workshop
Interests: choose any that apply

Try selecting both interests, then only one. Notice the repeated name and the missing unchecked choice. Click a text label, try keyboard navigation and submit with a required field empty.

The inspection behaviour is supplied by JavaScript. In a real application, the action endpoint receives and processes the data. You will learn more about scripts and servers later.

CHOOSE THE FORM TOOL

Match each purpose with its element, attribute or control type.

Terminology

Terminology

form

A container for controls used to collect and submit user input.

label

Text associated with a control to explain its purpose.

id

A unique identifier for an element in the page.

name

The key used for a control’s submitted entry.

value

The submitted value of a control or option.

input

A control whose type determines its input behaviour.

Radio group

Radio buttons sharing a name, allowing one selected choice.

Checkbox

An independent control with checked and unchecked states.

select and option

A menu control and the choices it contains.

textarea

A control for multi-line text input.

fieldset and legend

Elements that group related controls and name the group.

action and method

Attributes defining the submission destination and how the form data is sent.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. Which correctly connects a label to a text input?
2. Which statements about id and name are correct?
3. Which controls suit these needs?
4. What makes two radio buttons part of the same group in one form?
5. Which statements about submission are true?
6. Which button is appropriate for showing extra information without submitting?
7. Which statements about GET and POST are correct?
8. Which statements about forms are correct?

EXPLAIN, PREDICT AND REPAIR

Write your answers before revealing the samples.

1. Write a labelled text input for a team name. Include an id and a name.

2. A label has for="email" but its input has id="contact". Explain the problem and repair it.

3. Two workshop radio buttons can both remain checked. What should you investigate?

4. Predict the entries when workshop=code is selected and both interest checkboxes are checked.

5. Explain why an HTML signup form does not automatically email the club leader.

6. Explain why POST and a password input do not by themselves make a secure login system.

BUILD | A CLUB APPLICATION FORM

Create signup.html as a complete HTML document. Add a clearly labelled form for a fictional club. Include a text input, email input, one radio group, independent checkboxes, a select and a textarea.

This task builds the interface. You do not yet need a working submission endpoint. Use fictional data when testing and do not treat the form as a live signup service.

Extension: add a numeric team-size field and explain why a student identifier should generally remain a text field.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for 02.2 Forms, Labels, Inputs and Buttons is coming soon. Complete the club-form task and keep your HTML file for the next lessons.