Lesson 20 of 36
HTML Tutorial

Form Basics

How the form element collects data, and what action, method and name really do.

3 min readBeginnerWeb FundamentalsHTML Forms

Forms let visitors talk back to your site: place an order, send a message, log in. In this section we build forms for the Brew and Byte cafe. First, the foundations.

The form element#

Everything lives inside a <form>. Its two most important attributes decide where the data goes and how it travels.

html
<form action="/subscribe" method="post">
  <label for="email">Email address</label>
  <input type="email" id="email" name="email">
  <button type="submit">Subscribe</button>
</form>
  • action is the URL that will receive the data when the form is submitted. If you omit it, the form submits to the current page.
  • method is the HTTP method. The two you need are get and post.

GET versus POST#

GETPOST
Where data goesIn the URL, as a query stringIn the body of the request
Visible to the userYesNo (but not secret!)
BookmarkableYesNo
Good forSearching, filteringCreating or changing data, logins, long text

A search box uses GET, so the results page can be shared as a link. A sign-up or order form uses POST. Neither method is encryption. Protect real secrets by serving your site over HTTPS.

The name attribute#

When the form is submitted, the browser sends each field as a name and value pair. A field without a name is not sent at all. The name is the key your server code will look up.

html
<form action="/search" method="get">
  <input type="search" name="q" id="q" aria-label="Search the menu">
  <button>Search</button>
</form>

Typing "mocha" and submitting takes you to an address ending in /search?q=mocha.

What submitting does#

A form is submitted when the user presses a submit button or presses Enter inside a text field. The browser then:

  1. Checks built-in validation rules (covered in a later lesson).
  2. Collects every named field's value.
  3. Sends the request to action with the chosen method.
  4. Displays the server's response.

You will not process data on the server in this tutorial (that is the job of a back-end language), but you can see everything the browser sends using Developer Tools, in the Network tab.

Other useful form attributes#

  • autocomplete="on" or "off" hints whether the browser may suggest saved values. Individual fields can use specific tokens like autocomplete="email" to help password managers and autofill.
  • novalidate turns off the browser's built-in validation.
  • enctype="multipart/form-data" is required when a form uploads files.
  • target="_blank" shows the response in a new tab.
html
<!-- a file upload form needs POST and the multipart encoding -->
<form action="/upload" method="post" enctype="multipart/form-data">
  <label for="photo">Your latte art photo</label>
  <input type="file" id="photo" name="photo" accept="image/*">
  <button type="submit">Upload</button>
</form>

Common mistakes#

  • Forgetting the name attribute, then wondering why the server received nothing.
  • Using GET for passwords or large messages.
  • Omitting a submit button, leaving keyboard users with no obvious way to send.
  • Assuming POST makes data private.
  • Nesting one form inside another, which is not allowed.

Try it yourself#

Create a search form using method="get" with an input named q and a button. Submit it and read the address bar. Then create a newsletter form using method="post" and open the Network tab in Developer Tools to inspect the request. Remove the name attribute from one field and compare what is sent.

Recap#

A form has an action (where) and a method (how). Only fields with a name are submitted. Use GET for searches, POST for changes, and always validate on the server as well.

Check your understanding

Lesson quiz

Optional

A short quiz to check what you just learned.

Questions
4
Time limit
5 min
Pass mark
70%
Best score
-