Skip to content

Beginner · Forms, semantic HTML and accessibility · topic 10 of 64 · 30 min

Forms and inputs

Explanation

A form collects information from the user, such as a name, a mobile number or a choice of course. Each box is an input with a label that says what to type. When the user presses the submit button, the browser sends the answers to the address in the form's action.

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).

Think of it like this: Think of a paper application form at a bank. Each printed question is a label, each empty box is an input, and dropping the form in the box at the counter is the submit button. The address on the box is the action.

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.

html
<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>
Every input has a label (for = id) and a name

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

typeUse forWhat the phone shows
textNames, short answersNormal keyboard
emailEmail addressesKeyboard with @ and .
telPhone numbersNumber pad
numberQuantities, ageNumber keyboard with up/down
dateDates of birth, travel datesA date picker
passwordPasswordsHidden dots
fileUploading a photo or PDFFile 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.

html
<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>
A radio group in a fieldset, a select and a 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.

Tip: A label can also wrap the input: <label><input type="checkbox" name="terms"> I agree</label>. This works without for and id.
Careful: Never use placeholder text instead of a label. The placeholder disappears when the user starts typing, so they forget what the box was for.

Watch the lesson

Forms and inputs · 1/6

Forms = how a site listens

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)

Task 1 · Code · HTML

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.

Task 2 · Code · HTML

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

Question 1

1.How do you connect a label to an input?

Question 2

2.Which input type brings up a number pad for a mobile number?

Question 3

3.Only one of four categories may be chosen. Which control fits best?

Question 4

4.What happens to an input that has no name attribute when the form is submitted?

Question 5

5.Which method should a sign-up form with personal details use?

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.

← Previous: Lists and tables