Thuta Learning
HTML
IntermediateWeb DevelopmentBeginner

Form Element

HTMLLesson 2130 min

What you'll walk away with

  • Predict the request a form makes on submit from its action and method
  • Explain why a control without a name attribute sends no data
  • Choose GET for queries such as search and POST for actions that change data
  • Write label for/id pairs and button types correctly

Explain it simply

Explain it simply

Think of a form as a paper form you post. Each box has a heading (its name) and the person fills in a value. The envelope carries the address to send it to (action) and how to send it (method), and when the "Send" button is pressed, the browser collects everything and delivers it to the server.

Login, contact, checkout and search boxes: most places where users talk back to a website are forms. A form can send data with HTML alone, without a single line of JavaScript.

Problem → Why → Solution

The problem: missing data and a password in the URL

A login form forgets to give the email input a name and uses method="get". On submit the server never receives the email and answers "email is required", while the password appears as /login?password=secret123 in the address bar, the browser history and the server logs. A placeholder stands in for a label, so once you start typing, nothing says what the box is for.

login.html
<form action="/login" method="get">
  <input type="email" placeholder="Email">
  <input type="password" name="password" placeholder="Password">
  <button>Log in</button>
</form>

Why does this happen?

The browser sends only controls that have a name, as name=value pairs; without a name, whatever was typed is skipped. method="get" puts the data in the URL's query string, so secret data such as a password stays wherever URLs are stored.

Core concept: a form sends name=value pairs to its action

On submit, the browser walks through the form's controls in order and builds the list of data to send, the form data set. A control is included only if it has a name and is not disabled. Checkboxes and radio buttons are included only when checked: an unchecked checkbox does not send "false", it sends nothing at all. The name is the key the server reads, which is different from id; id only matters inside the page, for labels and CSS.

method="get"method="post"
Where the data goesThe URL: /search?q=htmlThe request body
Bookmark / shareYesNo
Good forRead-only queries: search, filtersLogin, orders, anything that changes data

action is the URL that receives the data, resolved against the page URL just as in the file paths lesson; leave it out and the data goes to the current page's URL. Leave out method and it defaults to GET. POST data is encoded as application/x-www-form-urlencoded by default (name=Aye&city=Yangon), and uploading files needs enctype="multipart/form-data".

A button inside a form defaults to type submit, so a button that should not send anything, such as "Show password", needs type="button". Pressing Enter in a text field also submits the form. Forms must never be nested inside other forms.

After a successful submit the browser navigates to whatever the server returns, just like following a link. With POST, refreshing that result page asks to resend the data, which is why servers usually answer a POST with a redirect to a normal page (the Post/Redirect/Get pattern).

POST is not security

POST only keeps data out of the URL; it does not encrypt anything. You need HTTPS to protect data on the network, and because every browser-side check can be bypassed, the server must always validate the data again.

Mental model: collect, pack, send
  1. Submit: button click or Enter
  2. Collect every named control
  3. Pack: GET → URL, POST → body
  4. Send the request to the action URL

All the server receives is the name=value list; labels, placeholders and ids never arrive.

General pattern

html
<form action="/endpoint" method="post">
  <label for="field-id">Visible label</label>
  <input type="text" id="field-id" name="field_name">

  <button type="button">Does not submit</button>
  <button type="submit">Submit</button>
</form>

The label's for matches the input's id, and name is the key that reaches the server. Use type to separate buttons that submit from buttons that do not.

Example: a form that sends a name

Each of this form's four lines does one job: where and how to send, the field's visible name, the data's key, and the button that sends. The output shows the request that results when a user types "Aye" and presses Send.

html
<form action="/submit" method="post">
  <label for="name">Your name</label>
  <input type="text" id="name" name="name">
  <button type="submit">Send</button>
</form>

Code breakdown

  1. form<form action="/submit" method="post">

    Data will be sent with POST to /submit on the current site, inside the request body. Nothing appears in the URL.

  2. label<label for="name">Your name</label>

    for="name" connects to the input with id="name". Clicking the label focuses the input, and screen readers announce the input as "Your name".

  3. input<input type="text" id="name" name="name">

    id is for the label, name is for the server. They share a value here but do different jobs: delete name and the label still works, but no data is sent.

  4. button<button type="submit">Send</button>

    submit is the default type, but writing it out is clearer. Clicking it, or pressing Enter in the text field, submits the form.

You should see
A "Your name" label, a text box and a "Send" button appear on one line. Clicking the label focuses the text box. Typing "Aye" and pressing Send makes the browser send a POST /submit request with Content-Type: application/x-www-form-urlencoded and the body name=Aye.

A GET form: the search box

A search form is the classic GET example. The result URL can be bookmarked or sent to a friend, and refreshing it changes nothing on the server. The browser encodes spaces as +, so searching for "html tables" produces /search?q=html+tables. The role="search" attribute marks the form as a search landmark, so screen reader users can jump straight to it.

search.html
<form action="/search" method="get" role="search">
  <label for="q">Search lessons</label>
  <input type="search" id="q" name="q">
  <button type="submit">Search</button>
</form>

In real projects

Where this shows up in real projects

Product filters on online shops (category, price, size) are often GET forms, which is why a URL such as /shoes?color=red&size=42 can be shared. Actions that change data, such as checkout, login or posting a comment, use POST, and servers commonly add a security field such as a CSRF token to the form as a hidden input.

In frameworks such as React or Vue, JavaScript often intercepts the submit event and sends the data with fetch(), but the form element, names, labels and button types stay underneath. Modern tools such as Remix and Next.js Server Actions are designed around plain HTML form submission, so forms can work even before the JavaScript has loaded (progressive enhancement). The rules in this lesson still apply when you use a framework.

Common mistakes

Common mistakes

1. Forgetting the name attribute

With an id but no name, the field works normally on screen but its data is missing on submit. When the server complains about a missing field, open the request payload in the DevTools Network tab and check that the name is there.

2. Every button in the form submits it

Buttons such as "Show password" or "Add another item" default to submit when they have no type, so clicking them sends the form and reloads the page. Write type="button" on every button that should not submit.

3. Using a placeholder instead of a label

A placeholder disappears as soon as you type, is not a reliable label for screen readers, and its pale text is hard to read. Connect every control to a label with for/id, and use placeholders only to show an example format.

Best practices

Best practices

  • Give each name exactly the key the server code expects (email, first_name), including letter case.
  • Give every control a visible label, and check that clicking the label focuses the control.
  • Add autocomplete values such as autocomplete="email" and autocomplete="name" so browsers can fill them in and save typing on phones.
  • Label the submit button with its outcome, such as "Create account" or "Send message", rather than "Submit".

Mini challenge

Mini challenge: a newsletter sign-up form

Write a newsletter sign-up form that POSTs to /subscribe, where it is clear which button submits.

  • action="/subscribe" and method="post"
  • Name (name="name") and email (type="email", name="email") inputs, each tied to a label with for/id
  • A "Send me the weekly digest" checkbox (name="weekly", value="yes")
  • A Subscribe button with type="submit" and a "Preview email" button with type="button" that does not submit
subscribe.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Newsletter</title>
</head>
<body>
  <h1>Join the newsletter</h1>
  <!-- TODO: form that POSTs to /subscribe -->
    <!-- TODO: label + text input for the name -->
    <!-- TODO: label + email input -->
    <!-- TODO: checkbox for the weekly digest, with its label -->
    <!-- TODO: a Preview button that does NOT submit, then a Subscribe button -->
</body>
</html>

Expected: Filling in "Aye" and "aye@example.com", checking the box and pressing Subscribe sends POST /subscribe with the body name=Aye&email=aye%40example.com&weekly=yes. With the box unchecked, weekly is absent. Pressing "Preview email" sends no request.

Show hint

A checkbox's label usually follows the checkbox; with for/id the connection works wherever it sits. weekly=yes is sent only when the box is checked, and without a value attribute it would send weekly=on.

Show solution
subscribe.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Newsletter</title>
</head>
<body>
  <h1>Join the newsletter</h1>
  <form action="/subscribe" method="post">
    <p>
      <label for="sub-name">Name</label>
      <input type="text" id="sub-name" name="name" autocomplete="name">
    </p>
    <p>
      <label for="sub-email">Email</label>
      <input type="email" id="sub-email" name="email" autocomplete="email">
    </p>
    <p>
      <input type="checkbox" id="sub-weekly" name="weekly" value="yes">
      <label for="sub-weekly">Send me the weekly digest</label>
    </p>
    <button type="button">Preview email</button>
    <button type="submit">Subscribe</button>
  </form>
</body>
</html>

Quick quiz 1 / 3

A <form action="/search" method="get"> contains <input name="q">. The user types "red shoes" and submits. What does the browser request?

Quick quiz 2 / 3

A user types "Yangon" into <input type="text" id="city"> (no name) and submits the form. What happens?

Quick quiz 3 / 3

Which setup suits a change-password form best?

What to remember

  • On submit, the browser sends every named, enabled control to the action URL as name=value pairs.
  • GET puts data in the URL, for search and filters; POST puts it in the body, for actions that change data.
  • Tie each label's for to an input's id, and write type="button" on buttons that must not submit.
  • POST is not encryption: HTTPS and server-side validation are always required.

HTML Living Standard — Forms — WHATWG

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Form Element | Thuta Learning