{module 3}
Forms: asking a visitor for information
Your first page in HTML20 min read
What you will be able to do
- Build a form in which every field has a label, a type and a name.
- Use
requiredand the field types to let the browser check what a visitor typed. - Explain where a form's answers go, and why a form needs a program to receive them.
Why it matters
Olive Grove Stays rents four holiday rooms among olive trees near Kalamata, Greece. Its owner, Eleni Papadopoulou, asks guests to email her about a room. Most emails leave something out. "Is there a room in June?" has no dates, no number of nights and no room. Eleni writes back to ask. The guest answers two days later, and by then many have booked somewhere else.
A form asks for each piece of information by name: the dates, the nights, the room. The browser can even refuse to send it while a needed answer is missing. This lesson builds that form. It also shows something Eleni did not know: a form on its own sends its answers nowhere useful.
The idea
A form is a set of fields and a button
A form collects what a visitor types and sends it all at once. The form element holds the fields and the button that sends them. Each field is a place to type or choose one answer.
Input fields and their types
The most common field is input. Like img, it has no content and no closing tag. Its type attribute says what kind of answer it takes:
type="text": any text, such as a name.type="email": an email address. On a phone, the keyboard shows the@key.type="date": a date. The browser shows a calendar to choose from.type="number": a number. The browser shows small arrows to go up and down.
A value attribute gives a field a starting answer, which the visitor can change.
A label for every field
A label is the visible name of a field. Its for attribute must be exactly the same as the field's id:
<label for="email">Your email address</label>
<input type="email" id="email" name="email" required />This connects the two. A screen reader reads the label when its user reaches the field, so they know what to type. And a click on the label puts the cursor in the field, which helps anyone using a small screen. The p around each pair puts each field on a line of its own.
A name for every answer
The name attribute is the name under which the answer is sent. The id is for the label, and the name is for the answer. A field needs both, and they can be the same word.
A choice from a list, and a longer answer
A select offers a fixed set of choices, as a menu. Each choice is an option:
<select id="room" name="room">
<option value="garden">Garden room, $70 a night</option>
<option value="sea">Sea view room, $95 a night</option>
</select>The visitor sees the text of each option. What is sent is its value. Until the visitor chooses, the first option is chosen.
A textarea is a field for several lines of text. Unlike input, it has a closing tag. Its rows attribute sets how many lines tall it is.
A button that sends
<button type="submit">Send my question</button>A button with type="submit" sends the form. Its text says what will happen.
What the browser checks for you
Before it sends, the browser checks every field.
- A field with
requiredmust not be empty.requiredis an attribute with no value: writing the word is enough. - An
emailfield must contain something shaped like an email address: some text, an@, and more text. - A
numberfield accepts only a number.
If a check fails, the browser does not send anything. It puts the cursor in the first wrong field and shows a short message beside it. The browser writes that message itself, in the language the browser is set to, so it differs from one browser to another.
Worked example
Make a folder called olive-grove with an index.html file, and type this page:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Ask about a room, Olive Grove Stays</title>
</head>
<body>
<main>
<h1>Ask about a room</h1>
<p>Tell us when you would like to stay. Eleni replies within one day.</p>
<form>
<p>
<label for="guest-name">Your name</label>
<input type="text" id="guest-name" name="guest-name" required />
</p>
<p>
<label for="email">Your email address</label>
<input type="email" id="email" name="email" required />
</p>
<p>
<label for="arrival">Arrival date</label>
<input type="date" id="arrival" name="arrival" required />
</p>
<p>
<label for="nights">Number of nights</label>
<input type="number" id="nights" name="nights" value="2" />
</p>
<p>
<label for="room">Room</label>
<select id="room" name="room">
<option value="garden">Garden room, $70 a night</option>
<option value="sea">Sea view room, $95 a night</option>
</select>
</p>
<p>
<label for="message">Anything we should know?</label>
<textarea id="message" name="message" rows="4"></textarea>
</p>
<button type="submit">Send my question</button>
</form>
</main>
</body>
</html>Save it and open it in your browser. These are the labels, one beside each field:
Your name
Your email address
Arrival date
Number of nights
Room
Anything we should know?The nights field starts at 2, and the room menu starts at the garden room. Click the words "Your email address". The cursor jumps into the email field, because the label is connected.
Send it and read where it went
Fill in the form as a guest would: the name Nikos Andreou, the email nikos@example.com, the date 14 June 2026, three nights, the sea view room, and the message "We arrive late". Press "Send my question".
The form seems to empty itself. Look at the address bar. This is the address the checker's browser was at. On your computer, your own folders take the place of /home/you/project:
file:///home/you/project/index.html?guest-name=Nikos+Andreou&email=nikos%40example.com&arrival=2026-06-14&nights=3&room=sea&message=We+arrive+lateThe form has no instructions about where to send its answers, so the browser sent them to the page's own address, and opened the page again. The answers are after the ?.
- Each answer is its
name, an=sign and what was typed. The answers are joined by&. - An address cannot contain spaces, so each space became
+. The@became%40, which is how an address writes that sign. - The date was sent as
2026-06-14, year first, whatever your browser showed you. - For the room, the browser sent the
value,sea, not the text the guest saw.
Let the browser refuse
Open the page again from the file, so that the form is empty and the address has no ?. Press the button without typing anything. Nothing is sent: the address does not change. The browser puts the cursor in the name field and shows its message there. The checker cannot see that message, but it can see the address, and how many fields failed their check:
file:///home/you/project/index.html
3Three fields are required, and all three are empty. Now fill in the name and the date, but type nikos.example.com as the email, with no @. The address still does not change:
file:///home/you/project/index.htmlLast, type nikos@x as the email. The browser sends it:
file:///home/you/project/index.html?guest-name=Nikos+Andreou&email=nikos%40x&arrival=2026-06-14&nights=2&room=garden&message=The browser checks the shape of an address, not whether it exists. It is a help for honest visitors who make a typing mistake, nothing more.
Why a form needs a program
Look again at where the answers went: into the address of a file on your own computer. Nothing was saved, and Eleni received nothing.
For a form to reach Eleni, its answers must go to a server, and a program on that server must receive them: save them, or email them to her. A form's action attribute names the address of that program. You will not write one in this course. GitHub Pages, where you publish, only sends files to visitors. It runs no program that could receive a form.
That program must also check every answer again. Anyone can send a server anything they like, without using your form at all, so the browser's checks protect nothing on their own.
On a practice page, then, a form shows what the business would ask, and a mailto: link from lesson 3 is how a visitor really reaches the owner.
Common mistakes
A label that is not connected
<label for="e-mail">Your email address</label>
<input type="email" id="email" name="email" required />The page looks the same. Click the label, then count the fields that have the cursor:
0for="e-mail" and id="email" differ by one hyphen, so the label belongs to no field. A screen reader reaches a field with no name. Copy the id into for, so that they cannot differ.
A field with no name
<input type="email" id="email" required />Type a name and an email address, and send:
file:///home/you/project/no-name.html?guest-name=Nikos+AndreouThe guest typed an email address, and it was never sent. Only a field with a name is sent. Eleni would have a name and no way to reply.
Think it through
Ana is asked to add a form to the page of Lotus Laundry, the laundry from lesson 4, for hotels that want their laundry collected. It needs the hotel's name, the room number, the day to collect, and the service.
Understand. In: four answers from a hotel. Out: a form where each answer has a label and a name, the needed answers cannot be left empty, and the service is chosen from the laundry's own list.
Break down. One field for each answer, the button, and the checks.
Plan. Hotel: text, required. Room number: text, because some rooms are numbered "12B". Collection day: date, required. Service: a select with one option for each service in the price table. Then a submit button that says "Ask for a collection".
Build. She writes the form with the first field and the button, opens the page, and sends it once to see the first answer in the address. Then she adds one field at a time.
Test. She fills in every field and sends. The address shows four name=value pairs. The odd case: she leaves the date empty and sends again. The address does not change, and the cursor goes to the date.
Reflect. The room number looked like a number, but "12B" is not one. Choose a type for the answers people really give, not for what the field is called.
At work
Before you build a form, say back to the client what you are going to build, in their words. Eleni asked for "a booking form". A careful developer replies:
To check I have understood: guests will ask about a stay, not book and pay on the page. The form asks for their name, email address, arrival date, number of nights, the room and a message. The answers need to reach you by email. Is that right, and is anything missing?
Eleni answers: "Yes. And the number of guests: the garden room sleeps only two." That is a field the developer would have missed. The message also brings out the hard part early. "Reach you by email" needs a program on a server, which a practice page on GitHub Pages does not have. It is better to say this before building than after.
Your tasks
Task 1: Build the form and read the address
Build the worked example. Send it with your own answers, then copy the part of the address after the ? and find each answer in it.
You are done when you can point to six name=value pairs, and say why one of them contains + and another contains %40.
Task 2: Add the missing field
Add the field Eleni asked for in "At work": the number of guests, as a number field with a label, a name and required.
You are done when the browser refuses to send the form while the new field is empty, and sends a guests= pair once it is filled.
Task 3: The collection form
Build Ana's form from "Think it through" on a new page, collection.html, in your lotus-laundry folder from lesson 4.
You are done when a click on every label puts the cursor in its field, the browser refuses to send without the hotel and the date, and the address shows four pairs when every field is filled.
Recap
- A
formholds fields and abuttonwithtype="submit". - Every field has a
labelwhoseforequals the field'sid, and anameunder which its answer is sent. requiredand the typesemail,dateandnumberlet the browser refuse an answer that is missing or the wrong shape.- With no program to receive it, a form sends its answers to the page's own address, where nobody reads them.
Go deeper
- MDN: Your first form builds a form step by step, with labels and names.
- MDN: Client-side form validation explains what
requiredand the field types check, and why a server must check again. - MDN: The input element lists every input type, with an example of each.
- MDN: Sending form data explains where a form's answers go, and what
actiondoes.