HTML and CSS · Chapter 4 of 12
Forms
By the end of this chapter you can build a form that a person can fill in and a screen reader can read: fields of the right type, each with a label joined to it and a name for what is sent, a select, radio buttons in a fieldset, a checkbox, a button, and the browser's own checks that stop a form before it is sent. You can also say exactly what the browser sends when someone presses the button. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: HTML: the structure
- 4 tasks
- HTML and CSS
- In the all labs plan
The first 2 chapters of HTML and CSS are free. This one comes with the all labs plan.
Why you need this
Most of Datta's 150 calls a day are questions that could be written down: a refund for a show that clashed with a wedding, how to reach Sahyadri Lawns, whether a ticket can move to Sunday. Ira's design has a "Write to us" page so people can ask at any hour, and Datta answers the next morning with the venue and the topic in front of him. Datta has also added a box to the home page for the week's listings by email, and in a week of testing it has sent him nothing at all.
Everything so far only shows things. A page that asks a question needs elements a person can type into and choose from, and a way to send what they wrote.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
contact.html: the form and its text fields
On contact.html, under the paragraph, build a form with action="/contact" and method="post". It holds, in this order: a text field labelled Name, named name; an email field (type="email") labelled Email, named email; a phone field (type="tel") labelled Phone, named phone; a textarea labelled Message, named message; and last a button that says Send.
Each label is joined to its field: give the field an id and the label a for with the same id (or put the field inside its label). The label's text is exactly Name, Email, Phone and Message. The button sends the form, so it has no type of its own. Keep the page's title, Write to us: Parda.
- Task 2
contact.html: the venue, the topic and the listings
Add three parts to the form. After Phone, a select labelled Venue, named venue, with one option a venue, in this order: Kala Talkies, Nilambari Cinema, Moti Mahal Talkies, Chitra Mandir, Shalini Picture House, Rangdeep Studio Theatre, Natya Ghar, Kothrud Kala Kendra, Sur Sangam Hall, The Attic, Kattaa Cafe Stage, Sahyadri Lawns. After Venue, a fieldset whose legend is What is it about?, holding four radio buttons, all named topic, each inside its label: Tickets (value tickets), A refund (value refund), Getting there (value getting-there), Something else (value something-else). After Message, a checkbox named listings with value yes, inside its label Send me the week's listings.
The form's parts are then, in order: Name, Email, Phone, Venue, What is it about?, Message, Send me the week's listings, and the button Send.
- Task 3
contact.html: the browser's own checks
Make the browser stop the form before it is sent when it cannot be answered. Name, Email and Message must be filled in (required). Email is an email field, so it also refuses something that is not an email address. Phone may be left empty, but when it is filled in it takes exactly 10 digits: pattern="[0-9]{10}". Message takes at most 500 characters (maxlength="500").
Press Send in the Preview with a field empty: the browser shows its own message and sends nothing.
- Task 4 · interview question
Interview: index.html, a form with no labels and no names
At the bottom of index.html, Datta's listings form has two fields with grey placeholders and no labels, and it sends nothing: the Preview's console says "with nothing in it (no field has a name)". Fix it so that a label Name is joined to the first field, named name, and a label Email is joined to the second field, an email field (type="email"), named email. Filled in with Asha and asha@example.com, the form then sends name=Asha, email=asha@example.com.
Keep the form's action="/listings", its method and its button, Send me the listings.
Ira will ask: what does a label do, and what does the browser send when you press Send?
What you will learn
- <form action method>: the fields and the button, and where and how they are sent.
- <label>: the field's name for people, joined to the field; why a placeholder is not one.
- <input type>, <textarea>, <select>: text, email, phone, number, a message, a choice from a list.
- Radio buttons, checkboxes, <fieldset> and <legend>: one choice from a group, a yes or no, and a group's caption.
- name and value: what the browser sends, as name=value.
- The browser's checks: required, type="email", min and max, pattern, maxlength.
More in HTML: the structure
Build "Forms" yourself
Parda is launching its first website for Pune's own cinemas and stages. You build it, page by page. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of HTML and CSS are free. This one comes with the all labs plan.