Start.dev
Back to blog

HTML forms explained for beginners

HTML forms explained step by step: the form tag, inputs and labels, checkboxes, selects, built-in validation, and a complete contact form.

Ariel Weinberger
Ariel Weinberger ยท 6 min read

How do you make a web page that lets people type something in and send it?

Short answer: wrap your fields in a <form> element, give each field a <label> and a name, and add a submit button. The browser collects the values and sends them to the address in the form's action.

<form action="/subscribe" method="post">
  <label for="email">Email</label>
  <input id="email" name="email" type="email" required>
 
  <button type="submit">Subscribe</button>
</form>

That is a complete, working form. This post explains each piece, the input types worth knowing, how to group checkboxes and radio buttons, and the validation the browser gives you for free. If tags and attributes are new, read HTML links and anchor tags first.

The form element

<form> is a container. Everything inside it that has a name gets sent when the form is submitted. What Is A Form (opens in a new tab) introduces it in the course.

Two attributes control where the data goes:

  • action is the URL to send it to. Leave it out and the form submits to the current page.
  • method is how to send it. get puts the values in the URL, like ?q=shoes, which suits searches. post sends them in the request body, which suits anything that creates or changes data.

Inputs and labels

An <input> is a single field. A <label> tells people, and screen readers, what that field is for.

Connect them by giving the input an id and the label a matching for:

<label for="name">Your name</label>
<input id="name" name="name" type="text">

Now clicking the label focuses the input, and assistive technology reads "Your name" when the field is focused. Text Inputs And Labels (opens in a new tab) has the exercise.

Keep the two attributes straight:

  • id connects the label to the input on the page.
  • name is the key the value is sent under. A field without a name is not submitted at all.

You will often use the same word for both, which is fine.

Input types worth knowing

The type attribute changes how the field behaves, what keyboard phones show, and what the browser checks.

TypeWhat it is forWhat you get for free
textShort free textA plain box
emailEmail addressesFormat check and an email keyboard on phones
passwordPasswordsHidden characters
numberNumbersUp and down controls, min and max
telPhone numbersA number pad on phones
dateDatesA date picker
checkboxYes or no, or pick severalA tick box
radioPick exactly one of severalLinked round buttons

Picking the right type is the easiest accessibility and usability win in a form. Input Types (opens in a new tab) goes through them one by one.

Checkboxes and radio buttons

Checkboxes are independent. Each one can be on or off:

<input id="news" name="newsletter" type="checkbox" value="yes">
<label for="news">Send me the newsletter</label>

Radio buttons work as a group. Give them all the same name, and only one can be selected:

<fieldset>
  <legend>Experience level</legend>
 
  <input id="lvl-new" name="level" type="radio" value="new">
  <label for="lvl-new">Brand new</label>
 
  <input id="lvl-some" name="level" type="radio" value="some">
  <label for="lvl-some">Some experience</label>
</fieldset>

<fieldset> and <legend> give the group a heading, so the options make sense when read aloud. The value is what gets sent, so the server receives level=some, not the label text. See Checkboxes And Radio Buttons (opens in a new tab).

Textarea and select

For longer text, use <textarea>. For a dropdown, use <select> with <option> elements:

<label for="topic">Topic</label>
<select id="topic" name="topic">
  <option value="">Choose one</option>
  <option value="billing">Billing</option>
  <option value="courses">Courses</option>
</select>
 
<label for="message">Message</label>
<textarea id="message" name="message" rows="5"></textarea>

Unlike <input>, <textarea> has a closing tag. Any text between the tags becomes its starting value. Textarea And Select (opens in a new tab) has both.

Validation the browser does for you

Before writing any JavaScript, you can make the browser check input with a few attributes:

<input name="email" type="email" required>
<input name="age" type="number" min="13" max="120">
<input name="username" minlength="3" maxlength="20">
  • required stops the form submitting while the field is empty.
  • type="email" checks the value looks like an email address.
  • min, max, minlength and maxlength set limits.

If anything fails, the browser shows a message and doesn't submit. Validating With HTML (opens in a new tab) covers it.

One warning: this is a convenience for the person filling in the form, not security. Anyone can bypass it, so the server must always check the data again.

Putting it together: a contact form

<form action="/contact" method="post">
  <label for="c-name">Name</label>
  <input id="c-name" name="name" type="text" required>
 
  <label for="c-email">Email</label>
  <input id="c-email" name="email" type="email" required>
 
  <label for="c-topic">Topic</label>
  <select id="c-topic" name="topic" required>
    <option value="">Choose one</option>
    <option value="question">A question</option>
    <option value="feedback">Feedback</option>
  </select>
 
  <label for="c-message">Message</label>
  <textarea id="c-message" name="message" rows="5" minlength="10" required></textarea>
 
  <button type="submit">Send</button>
</form>

Every field has a label, a name, and a sensible type. The empty first option plus required on the select means people have to actually choose a topic. You build a version of this in Contact Form (opens in a new tab).

Common mistakes

  • Leaving out name. The field looks fine but is never sent. This is the most common "my form isn't working" bug.
  • Using placeholder instead of a label. Placeholder text disappears as soon as someone types, and many screen readers treat it inconsistently. Use a real <label>.
  • A label for that doesn't match the input id. They must be spelled exactly the same.
  • Giving radio buttons different names. Then they aren't a group, and people can select all of them.
  • Trusting browser validation as security. It helps users. It doesn't protect your server.
  • Using <div> as a button. A real <button type="submit"> works with the keyboard and with Enter in a text field.

Quick recap

  • <form> wraps the fields. action says where to send them and method says how.
  • Every field needs a name to be sent, and a <label> connected by for and id.
  • Pick the right input type. It improves validation, keyboards and accessibility.
  • Radio buttons share one name. Group them with <fieldset> and <legend>.
  • required, min, max and friends give you validation without JavaScript, but the server still checks.

Frequently asked questions

What is a form in HTML?

A form is a section of a page that collects input from the user, such as text, choices and checkboxes, and sends it somewhere when it is submitted. It is made with the <form> element and the fields inside it.

What is the difference between GET and POST in a form?

GET adds the form values to the URL, which is useful for searches you might bookmark or share. POST sends them in the request body, which is the right choice for sign-ups, messages and anything that changes data.

Why isn't my form sending a field?

The field probably has no name attribute. Only fields with a name are included when the form submits. Disabled fields are also left out.

Do I need JavaScript to make a form work?

No. A plain HTML form with an action submits on its own. You add JavaScript when you want to handle the submit without reloading the page, which JavaScript addEventListener shows how to do.

Should I use placeholder or label?

Use a label every time. A placeholder can be an extra hint, like an example format, but it is not a replacement for a label.

Practice

Work through the forms section of HTML Basics in order: What Is A Form (opens in a new tab), Text Inputs And Labels (opens in a new tab), Input Types (opens in a new tab), then build the Contact Form (opens in a new tab). Each one runs in your browser, so there is nothing to install. Afterwards, make your form look good with CSS basics.

Keep going

HTML Basics (opens in a new tab) takes you from your first tag to links, semantic layout, forms and a small multi-page site. When you want your forms to respond without a page reload, JavaScript DOM (opens in a new tab) covers reading form values and validating them in JavaScript.

Level up your coding skills

Jump into interactive courses and start building real-world projects.

Start learning

Related posts

How the HTML anchor tag works: full URLs, relative paths, jump links to a section, opening a new tab safely, and the small mistakes that break links.
Ariel Weinberger
Ariel Weinberger ยท 6 min read
Learn addEventListener step by step: handle clicks, typing and form submits, use the event object, and avoid the mistakes that break listeners.
Ariel Weinberger
Ariel Weinberger ยท 6 min read
Learn the CSS box model with clear diagrams and code: content, padding, border, and margin, plus box-sizing, margin collapse, and DevTools tips.
Ariel Weinberger
Ariel Weinberger ยท 8 min read