Until now, every page of The Blue Cup has spoken in one direction only: the site shows information (the menu, the opening hours, the news) and the visitor reads it. A form reverses the flow: it lets the visitor send data to the site, whether to book a table, sign up for a workshop, search the news or write a message. In this lesson you will understand what a form is and how its data travels from the browser to the server, you will meet the form element with its action and method attributes, you will create your first text field with name and a submit button, and you will see with your own eyes exactly what is sent when you click it. By the end, bookings.html will no longer say "Very soon…" and will have its first form, still minimal, which you will keep extending throughout the module.

Contents

  1. What a form is
  2. The client-server cycle of a form
  3. The form element
  4. action and method: a first look
  5. The first field: a text input
  6. The submit button
  7. What travels on submission: name=value pairs
  8. id versus name
  9. A label for the field: label (basic use)
  10. How to test the submission without a server
  11. Project: the first form in bookings.html

What a form is

A form is an area of the page that collects data from the user through controls (text fields, checkboxes, drop-down lists, buttons…) and sends it somewhere to be processed.

You use them every day, even if you don't notice:

  • The search box of any search engine.
  • The sign-in screen of your email.
  • The checkout page of an online shop.
  • The "Subscribe" button of a newsletter.

They all share the same underlying structure:

Part What it is In HTML
Container Groups all the controls that are sent together form
Controls Where the user types or chooses input, textarea, select…
Names The "internal label" of each piece of data sent name attribute
Submit button Triggers the submission button or input type="submit"
Destination Where the data goes and how action and method attributes

An important point right from the start: HTML only builds the form and sends it. What happens afterwards (saving the booking, sending a confirmation email, checking whether there are free tables) is done by a program on the server, written in another language (PHP, Python, JavaScript with Node.js, Java…). That program is outside the scope of this course; in the lesson Submitting Forms you will see roughly what it does and what alternatives you have if you don't want to write it yourself.

The client-server cycle of a form

In the lesson What Is HTML? you saw that the browser (the client) requests pages from the server, which sends them back. A form adds a step: the client not only requests, it also sends data along with its request.

sequenceDiagram
    participant U as User
    participant N as Browser (client)
    participant S as Server
    N->>S: 1. Requests bookings.html
    S-->>N: 2. Returns the page with the form
    U->>N: 3. Fills in the fields and clicks Send
    N->>S: 4. Sends the data to the action URL (using the method in method)
    S->>S: 5. A program processes the data
    S-->>N: 6. Returns a response (for example, "Booking received")
    N-->>U: 7. Displays the response

Step by step:

  1. The browser requests bookings.html, just like any other page.
  2. The server returns it; it contains a form.
  3. The user types in their details and clicks the button.
  4. The browser collects the value of each control, packages it and sends it to the address given in the form.
  5. On the server, a program receives that data and does whatever it needs to do.
  6. The server responds with a new page (usually a confirmation or an error message).
  7. The browser displays that response: it is a navigation, just like following a link.

Steps 1–4 and 7 are HTML's responsibility; steps 5 and 6 are the server's. In this module you will make sure that steps 3 and 4 work perfectly.

The form element

Every form starts with the form element, which wraps the controls that are sent together:

<form>
  <!-- the controls and the button go here -->
</form>
  • It is a block element: it takes up its own line, like a div.
  • It can contain almost any content: paragraphs, headings, lists… and, of course, controls.
  • Forms cannot be nested: a form cannot contain another form. If a page needs two forms (for example, a search and a subscription), they are placed one after the other, never one inside the other.
  • On its own it is invisible: it has no border or background. You will only see its controls.

action and method: a first look

The form needs to know two things: where to send the data and how. It has two attributes for this:

<form action="https://forms.thebluecup.example/bookings" method="get">
  …
</form>
Attribute Question it answers Value in the example If you omit it
action Where do I send the data? The URL of the program that will process it It is sent to the same page that contains the form
method How do I send it? get get is used

For now, this idea of the two methods is all you need:

  • get: the data travels attached to the URL, visible in the address bar. Very handy for learning, because you can see it.
  • post: the data travels inside the request, not in the URL. This is the right choice for personal data such as booking details.

In this lesson and the next ones you will use get precisely so that you can see what is sent. In the lesson Submitting Forms, at the end of the module, you will study both methods in depth and switch the booking form to post.

About the URL: forms.thebluecup.example is a fictitious address. Domains ending in .example are reserved for examples and will never respond. You use it on purpose: that way you can practise submitting without sending data to any real server.

The first field: a text input

The most common control is input. It is an empty element (it has no closing tag, like img or br) and its type attribute decides what kind of control it is. For a single line of text:

<input type="text" name="name">
  • type="text": a single-line text box. It is the default type: if you omit type (or write one the browser doesn't recognise), you get a text field. Even so, always write it: it makes your intention clear.
  • name="name": the name of the data item. It is the key under which the value will travel to the server. Without name, the field is not sent, even if the user has typed in it.

There are many other types of input (checkboxes, radio buttons, email, dates…). You will meet them in Form Elements: input, textarea and select and in HTML5 Input Types.

A useful attribute from the outset is value, which gives the field an initial value:

<input type="text" name="name" value="Anna">

The field will appear with "Anna" already typed in, and the user can delete or change it. What is sent is always the current value of the field at the moment the button is clicked, not the initial one.

The submit button

To submit the form you need a button. The recommended way is the button element:

<button type="submit">Send booking</button>
  • type="submit": clicking it submits the form. It is the default type of a button inside a form, but always write it so that there is no doubt.
  • The text between the tags is what appears on the button. It should say what will happen: "Send booking" is better than "Send", and much better than "OK".

There are other ways to submit:

  • With <input type="submit" value="Send booking">, the older way, which is still valid. Comparing the two is part of the next lesson.
  • Pressing Enter inside a text field: the browser treats it as clicking the submit button. This is called implicit submission, and it is the reason why in a search box you only need to type and press Enter.

What travels on submission: name=value pairs

This is the central concept of the whole module. On submission, the browser goes through the form's controls and, for each one that has a name, builds a name=value pair. If there are several pairs, it joins them with &.

With this form:

<form action="https://forms.thebluecup.example/bookings" method="get">
  <input type="text" name="name">
  <input type="text" name="city">
  <button type="submit">Send</button>
</form>

If the user types "Anna" in the first field and "Valencia" in the second, the browser navigates to:

https://forms.thebluecup.example/bookings?name=Anna&city=Valencia

Break it down into its parts:

Fragment What it is
https://forms.thebluecup.example/bookings The value of action
? Separates the address from the data. What follows is called the query string
name=Anna First pair: the field's name, =, and what the user typed
& Separator between pairs
city=Valencia Second pair

Some characters cannot travel as they are in a URL, so the browser encodes them:

  • Spaces become +: "Anna López" travels as name=Anna+L%C3%B3pez.
  • Accented letters, the ñ and other symbols become %XX sequences: the ó is %C3%B3.

It is the same encoding you saw with %20 in the mailto links in Special Links: Anchors, Email, Phone and Downloads. You don't have to do it yourself: the browser encodes on sending and the server decodes on receiving. You will see the details in Submitting Forms.

Two practical consequences:

  • The name is what the server understands. Whoever programs the server will expect, for example, a data item called name. If you write name="Name" (with a capital letter), to the server it is a different item: names are case-sensitive.
  • The text the user sees does not travel. The label "Your name" that you put next to the field is only for people; the server only receives name=Anna.

id versus name

You already know the global id attribute from Tags, Elements and Attributes. In forms, id and name live side by side, and it is easy to mix them up:

id name
What it is for Identifying the element within the page Naming the data item that is sent
Used by HTML itself (label, # anchors), CSS and JavaScript The server that receives the data
Does it travel on submission? No Yes, as the key of the name=value pair
Must it be unique? Yes, across the whole page Not necessarily (several radio buttons share a name; you will see this in the next lesson)
Is it required? No Yes, if you want the data to be sent

In practice, a text field usually gets the same value in both, because it makes life simpler:

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

But remember that they do different jobs: if you only set id, the field will be displayed and you will be able to type in it, but it will not be sent.

A label for the field: label (basic use)

A field with no text beside it is a mystery: what are you supposed to type? The label element is the visible label of a control. Its basic form links the label to the field through the id:

<label for="name">Name</label>
<input type="text" id="name" name="name">
  • The for attribute of label contains the field's id (not its name).
  • With that link in place, if you click the text "Name", the cursor jumps to the field. Try it: it is the quick test that you have written it correctly.

That is all you need for now. In the lesson Labels and Grouping: label, fieldset, legend and datalist you will see the other way of writing label, why it is essential for screen readers and how to group related fields.

How to test the submission without a server

You have no server, but you can still see what your form would send. There are two ways.

Option A: with the fictitious action

  1. Open the page with Live Server, fill in the field and click the button.
  2. The browser will try to go to forms.thebluecup.example and will show an error along the lines of "This site can't be reached". This is expected: the domain doesn't exist.
  3. Look at the address bar: you will see the full URL with ?name=…. That is exactly what the server would have received.

Option B: without action, against the page itself

If you temporarily remove action, the form is submitted to the same page. With Live Server you will see the page reload and the address change to something like:

http://127.0.0.1:5500/bookings.html?name=Anna

The page does nothing with the data (there is no program to read it), but you have completed the outward journey without errors.

In both cases you can use the browser's Back button to return to the form. In Submitting Forms you will learn to inspect the submission with the Network tab of DevTools, which will be essential once the data no longer appears in the URL.

Project: the first form in bookings.html

Open bookings.html. Since Module 3 it has had the logo, the menu, the h1 "Book your table" and a paragraph that promises: "Very soon you will be able to book online from this page." It's time to keep that promise.

1. Replace that paragraph with a short explanation and add the form below it:

    <h1>Book your table</h1>
    <p>Book a table for breakfast, the lunch menu or afternoon tea. We will confirm your booking by email.</p>

    <!-- Booking form: method="get" is temporary so we can see the data in the URL; it will switch to post in lesson 05-06 -->
    <form action="https://forms.thebluecup.example/bookings" method="get">
      <p>
        <label for="name">Name</label>
        <input type="text" id="name" name="name">
      </p>
      <p>
        <button type="submit">Send booking</button>
      </p>
    </form>

Explanation:

  • The introductory text says what can be booked and what will happen next (the confirmation by email). A form should always be preceded by whatever the user needs to know in order to fill it in.
  • The comment records a temporary decision. In a few lessons' time you will be glad of it.
  • action points to The Blue Cup's fictitious address for forms; method="get" lets you see the data in the address bar.
  • Each control sits inside a p. It is a simple way to make each row of the form take up its own line without CSS. (There are others: div, lists… The choice will be revisited in Module 11 when the form is integrated with the site's design.)
  • label with for="name" points to the field's id="name"; name="name" is the data item that will travel.

2. Save, open the page with Live Server and carry out these tests:

  1. Click the word "Name": the cursor should appear in the field.
  2. Type "Marta García" and click the button. You will see the connection error and, in the address bar, …/bookings?name=Marta+Garc%C3%ADa.
  3. Go back, clear the field and submit it empty: the URL ends in ?name=. The pair is sent anyway, with an empty value. (Preventing this will be the job of Form Attributes and Validation.)
  4. Type a name and press Enter without touching the button: it is submitted too.

It is a minimal form, but it already completes the full cycle. Over the next lessons it will grow to include the number of guests, the area, the date, the time, the email, the phone number, comments and acceptance of the privacy policy.

Common Mistakes and Tips

  • Forgetting name: mistake number one. The field is displayed and works, but its value doesn't appear in the URL. If a data item is "missing" on submission, check the name first.
  • Confusing for with name: label for points to the field's id. If clicking the label doesn't take you to the field, compare for and id character by character.
  • Nesting forms: a form inside another is invalid HTML and the browser will ignore the inner one. If you need two, put them one after the other.
  • Controls outside the form: an input written after </form> is not sent with that form (there is an exception with the form attribute, which you will see in Submitting Forms).
  • A name with spaces, accents or random capital letters: it works, but it makes life harder for the server. Use lower-case names, without accents and with underscores or hyphens if needed: name, booking_date.
  • Thinking that the connection error means the form is wrong: with the fictitious action, the error is expected. What matters is the URL you see.
  • Buttons with generic text ("Send", "OK", "Accept"): describe the action: "Send booking", "Search news", "Subscribe".
  • Tip: get into the habit of testing every form by submitting it and reading the URL. It is the quickest way to spot a forgotten or misspelt name.

Exercises

Exercise 1. Without testing it in the browser, write the URL that will be generated when this form is submitted if the user types "cold brew" in the field:

<form action="https://thebluecup.example/search" method="get">
  <input type="text" id="search-term" name="q">
  <button type="submit">Search</button>
</form>

Exercise 2. This form has three mistakes that stop it from working as expected. Find them and fix them:

<form action="https://forms.thebluecup.example/bookings">
  <label for="name">Name</label>
  <input type="text" id="customer-name" name="name">
  <label for="surname">Surname</label>
  <input type="text" id="surname">
</form>
<button type="submit">Send booking</button>

Exercise 3. Add a second text field to the form in bookings.html for the surname, with its label, between the name and the button. Submit it with "Marta" and "García" and note down the resulting URL.

Solutions

Solution 1.

https://thebluecup.example/search?q=cold+brew
  • The key is q, the name; the id (search-term) does not travel.
  • The space between "cold" and "brew" is encoded as +.
  • q (short for query) is the usual name of the search field on a great many sites.

Solution 2.

<form action="https://forms.thebluecup.example/bookings">
  <label for="name">Name</label>
  <input type="text" id="name" name="name">
  <label for="surname">Surname</label>
  <input type="text" id="surname" name="surname">
  <button type="submit">Send booking</button>
</form>
  1. for="name" didn't match id="customer-name": the label was not linked to the field. It is fixed by making both the same (changing the for would also work).
  2. The surname field had no name: it would never have been sent.
  3. The button was outside the form: clicking it did nothing. Now it is inside.

The missing method is not a mistake: it defaults to get.

Solution 3.

      <p>
        <label for="surname">Surname</label>
        <input type="text" id="surname" name="surname">
      </p>

The resulting URL is:

https://forms.thebluecup.example/bookings?name=Marta&surname=Garc%C3%ADa

The pairs appear in the order in which the fields appear in the code, joined by &, and the í is encoded as %C3%AD. (In the project you can keep this field or remove it; in the next lessons the form will use a single name field for the full name, as many restaurants do.)

Conclusion

In this lesson you have made the leap from pages that only display information to pages that collect it. You have seen that a form is a form container with controls and a button, that action says where the data goes and method how it travels, and that the browser turns each control with a name into a name=value pair that you can read in the URL when you use get. You have also learnt to distinguish id (for the page) from name (for the server), to link a label to its field with label for, and to test a submission without a real server. bookings.html now has a working form, even though it only asks for the name. In the next lesson, Form Elements: input, textarea and select, you will meet the rest of the basic controls (radio buttons, checkboxes, drop-down lists and text areas) and use them to ask how many guests are coming, whether they prefer indoors or the terrace and whether they have any comments.

HTML Course

Module 1: Introduction to HTML

Module 2: Text Formatting in HTML

Module 3: Links and Media in HTML

Module 4: Tables in HTML

Module 5: Forms in HTML

Module 6: HTML5 Semantic Elements

Module 7: Advanced HTML Techniques

Module 8: Integrating HTML and CSS

Module 9: Responsive Web Design

Module 10: Best Practices and Accessibility

Module 11: Project: Building a Complete Website

© Copyright 2026. All rights reserved