HTML forms: form, input, label and the submit button
Build your first working form step by step: the form element, inputs, labels, names and submit buttons.
In this lesson you will
- Explain what a form is and how it sends data
- Choose between GET and POST
- Connect labels, ids and names correctly
- Group fields with fieldset and legend
About 12 minutes
Why every website needs forms
Imagine Priya runs a small bakery. Customers keep phoning to order birthday cakes, and she is tired of writing names and dates on scraps of paper. She would love a page where people type their name, pick a date and press a button. That page is a form, and by the end of this lesson you will know how to build one.
Forms are how websites listen. Every search box, login screen, contact page, checkout and newsletter signup you have ever used is a form. Until now your pages only talked to visitors. Forms let visitors talk back.
The form element
A form is a container. Everything a visitor can fill in goes inside a <form> element, and the form knows two things: where to send the information and how to send it.
<form action="/order" method="post">
<!-- inputs go here -->
</form>
actionis the address that receives the data, for example a page on your server or the link a form service gives you.methodis how the data travels. You will choose betweengetandpost.
Think of the form as an envelope. The inputs are the letter, the action is the address on the front, and the method is whether you send it as a postcard or in a sealed envelope.
GET or POST?
With get, the data is added to the page address, so you might see ?q=cakes after the link. That is perfect for a search box, because people can bookmark or share the result. With post, the data travels hidden inside the request. Use it for anything private or anything that changes something, such as a contact message, an order or a password.
If the form only looks something up, use GET. If it sends something in, use POST.
Inputs, labels and names
The most common form control is the <input> element. It is an empty element, so it has no closing tag. Its type attribute decides what kind of box it is. The simplest is a line of text.
<form action="/order" method="post">
<label for="customer">Your name</label>
<input type="text" id="customer" name="customer">
<button type="submit">Send order</button>
</form>
Three details here do a lot of work, so let us go through them one at a time.
- The label tells people what the box is for. Clicking the label puts the cursor in the box, which is a big help on phones.
- The matching for and id. The label’s
formust equal the input’sid. That is the link between them. - The name. This is the label that travels with the data. When the form is sent, the server receives
customer=Priya. Without aname, the box is ignored completely.
The most frequent form bug is a missing name attribute. The box shows up and accepts typing, yet nothing arrives. If your form sends empty data, check the names first.
This form has a box but no label and no name. Add a label connected with for and id, and give the input a name of email.
<form>
<input type="text" id="email">
<button type="submit">Join</button>
</form>
Write <label for="email">Email</label> before the input and add name="email" to the input.
<form>
<label for="email">Email</label>
<input type="text" id="email" name="email">
<button type="submit">Join</button>
</form>
The submit button
A form needs something to press. The <button> element with type="submit" sends the form. A button with type="reset" clears it, though you will rarely want that because people press it by accident. Inside a form, a button with no type is a submit button, so always write the type to make your intent clear.
You may also see <input type="submit" value="Send"> in older code. It does the same job, but button is better because you can put text, icons or formatting inside it.
Grouping with fieldset
Long forms feel friendlier when they are split into parts. The <fieldset> element draws a box around related controls, and <legend> gives that box a title. Screen readers announce the legend before each control inside, which helps people understand the context.
<form action="/order" method="post">
<fieldset>
<legend>About you</legend>
<label for="n">Name</label>
<input type="text" id="n" name="name">
</fieldset>
<fieldset>
<legend>Your cake</legend>
<label for="f">Flavour</label>
<input type="text" id="f" name="flavour">
</fieldset>
<button type="submit">Send order</button>
</form>
Where does the data actually go?
Here is something courses often skip. HTML alone cannot store or email what people type. It only collects it and hands it over. Something on the receiving end must catch it. On a real site that is usually one of three things: a script on your own server written in a language such as PHP, a form service that gives you an address to put in action, or a WordPress plugin that handles everything for you. In this course we focus on building the form itself, which is the part you control in HTML.
If you build a form on your own computer and press the button, the page may reload or show an error. That is normal. There is no server listening yet, and the form has done its job correctly.
Try the whole thing
Build an order form for Priya’s bakery: a label and text input for the customer name, a label and text input for the cake flavour, and a submit button. Use for, id and name on both inputs.
<form action="/order" method="post">
<!-- add the two fields and the button -->
</form>
Copy the pattern from the first complete form: label, input with matching id and name, then a button.
<form action="/order" method="post">
<label for="name">Your name</label>
<input type="text" id="name" name="name">
<label for="flavour">Cake flavour</label>
<input type="text" id="flavour" name="flavour">
<button type="submit">Send order</button>
</form>
Habits of a good form
- Always pair every input with a visible label. A grey hint inside the box is not a substitute, because it vanishes when typing starts.
- Ask only for what you need. Every extra field makes people more likely to give up.
- Put each label and input on its own line, or group them with CSS later, so the form is easy to scan.
- Use clear button text such as “Send order” instead of the vague “Submit”.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Which element contains all the controls of a form?
<input><form><fieldset><label>
The form element wraps everything and holds the action and method.
Which method is best for a contact message?
getpostput- Either, it makes no difference
Post keeps the data out of the address and is meant for sending information in.
What connects a label to its input?
- Both having the same
class - The label’s
formatching the input’sid - Placing them in the same div
- The
nameattribute
for and id form the link. Clicking the label then focuses the input.
A form sends empty data even though people typed in it. What is the most likely cause?
- The inputs have no
name - The page has no heading
- The button is blue
- The form has a legend
Only controls with a name are included when the form is sent.
What does <legend> do?
- Adds a tooltip
- Gives a title to a fieldset
- Labels a button
- Hides a group
Legend is the caption of a fieldset and is read aloud by screen readers.
Summary
- A form wraps controls and holds the action and method.
- Use POST to send information and GET to look something up.
- Every input needs a matching label and a name.
- HTML collects the data; a server or service must receive it.
Questions
Do I need PHP to use a form?
To store or email the data yourself you need something on the server, which can be PHP, but you can also use a form service or a WordPress plugin. The HTML form itself needs no extra language.
Why does my form reload the page?
Submitting a form sends the data to the action address and loads the response. With no action, it reloads the same page.
Is placeholder text a replacement for a label?
No. Placeholder text disappears when someone starts typing and is poorly supported by screen readers. Always use a real label.
