Beginner · Forms, semantic HTML and accessibility · topic 10 of 64 · 30 min
Forms and inputs
Explanation
The form element
The <form> element wraps all the fields that belong together. Its action attribute is the address that receives the data, and method says how to send it: GET puts the answers in the URL (good for search), POST sends them in the body of the request (right for sign-ups, payments and anything private).
Labels and inputs
An <input> is a box the user types in or a control they pick. A <label> is the visible question. Connect them: give the input an id and the label a for attribute with the same value. Now tapping the label puts the cursor in the box, and screen readers read the label aloud.
<form action="/register" method="post">
<label for="full-name">Full name</label>
<input id="full-name" name="fullName" type="text" autocomplete="name">
<label for="mobile">Mobile number</label>
<input id="mobile" name="mobile" type="tel" autocomplete="tel">
<button type="submit">Register</button>
</form>The name attribute is the key sent to the server with the value, like fullName=Asha. An input without a name is not sent at all.
Input types
| type | Use for | What the phone shows |
|---|---|---|
| text | Names, short answers | Normal keyboard |
| Email addresses | Keyboard with @ and . | |
| tel | Phone numbers | Number pad |
| number | Quantities, age | Number keyboard with up/down |
| date | Dates of birth, travel dates | A date picker |
| password | Passwords | Hidden dots |
| file | Uploading a photo or PDF | File or camera chooser |
Choices: select, radio and checkbox
Use <select> with <option> for a long list (state, course). Use radio buttons when only one answer is allowed (gender, category): all radios in a group share the same name. Use checkboxes when many answers are allowed (languages known). Group related controls in a <fieldset> with a <legend> that asks the question.
<fieldset>
<legend>Preferred exam centre</legend>
<label><input type="radio" name="centre" value="delhi"> Delhi</label>
<label><input type="radio" name="centre" value="pune"> Pune</label>
</fieldset>
<label for="state">State</label>
<select id="state" name="state">
<option value="">Choose a state</option>
<option value="KA">Karnataka</option>
<option value="TN">Tamil Nadu</option>
</select>
<label for="about">About you</label>
<textarea id="about" name="about" rows="4"></textarea>Buttons
A <button type="submit"> sends the form. A <button type="button"> does nothing on its own; JavaScript gives it a job. Always write the type: a button inside a form is a submit button by default, which surprises many beginners.
Watch the lesson
Forms and inputs · 1/6
Forms are how websites listen to people: sign-ups, searches, bookings and payments.
Prefer a video by a teacher? Find free videos on “html forms tutorial labels inputs for beginners” on YouTube ↗
In real life: A scholarship registration form
A state scholarship portal asks for full name, date of birth (type="date"), mobile (type="tel"), category (radio buttons in a fieldset), college (a select list) and a photo (type="file"). Every field has a label, so students on cheap phones can tap the question to jump into the box, and the form is sent with POST because it holds personal details.
Practice (0/2 done)
Build a small contact form. It needs a text input for the name and an email input, each with a <label> connected by for and id, each with a name attribute, and a submit button.
Make a "Meal preference" question: a <fieldset> with a <legend>, and two radio buttons (veg and non-veg) that share the same name so only one can be chosen. Each radio must have a label.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.