# The form Element & Submission

> A form collects name-and-value pairs from its controls and sends them either as URL query parameters (GET) or in a request body (POST). Choosing the right method is not a style preference: it determines whether the data appears in browser history, can be bookmarked, or triggers a cache.

Subject: HTML | Lesson 25 of 42 | 6 min read
Interactive lesson: https://codewithanimation.in/html/form-and-submission
Previous: [Spanning & Accessible Tables](https://codewithanimation.in/html/table-spanning-and-a11y.md) | Next: [Labels & Text Inputs](https://codewithanimation.in/html/labels-and-text-inputs.md)

## What it is

`<form>` is the container that wraps interactive controls and defines how their data is packaged and delivered. The `action` attribute names the URL that receives the submission; if you omit it, the browser submits back to the current page. The `method` attribute determines the packaging: `GET` appends data to the URL as a query string (`/search?q=html+tables`), while `POST` places data in the request body where it is not visible in the URL.

Every control inside the form that has a `name` attribute contributes a `name=value` pair to the submission. Controls without a `name` are silently skipped: the browser neither warns you nor submits a placeholder. The `<button>` element defaults to `type="submit"`, which triggers the form. To make a button that does something in JavaScript without submitting, set `type="button"`.

## Why it matters

GET and POST have meaningfully different properties. A GET submission is idempotent: the same URL always returns the same result, so browsers cache it, users can bookmark it, and sharing the URL reproduces the exact search or filter. A search page at `/results?q=flexbox` is a real, shareable resource. POST is the right choice when the submission changes server state (creating a record, processing a payment) because caching or re-triggering the request would be destructive. A browser that warns you before reloading a POST result is protecting you from repeating that state change.

The `name` attribute is easy to forget and painful to debug. A field that looks correct in the browser may contribute nothing to the server, because without `name` the browser omits it. This is especially tricky for dynamically-added fields or controls nested inside custom components where the attribute can get dropped without any visible error.

## How it works

Set `method` to `GET` or `POST` on the `<form>`. Set `action` to the URL that should handle the data, or omit it to submit to the current page. Every `<input>`, `<select>`, `<textarea>`, and `<button type="submit">` inside the form that has a `name` will contribute to the submission. The value submitted for text inputs is the current content of the field; for checkboxes and radio buttons, only checked controls contribute.

For file uploads, set `enctype="multipart/form-data"`. The default encoding (`application/x-www-form-urlencoded`) serialises files as their filename with no content. If you need to send both files and text fields in one request, `multipart/form-data` handles both. A third encoding, `text/plain`, is rarely useful outside debugging.

A submit button can override the form's `action` and `method` on a per-button basis using `formaction` and `formmethod`. This is useful when a single form has two destinations: a "Save" button posting to `/draft` and a "Publish" button posting to `/publish`, both reading the same field values.

## Common mistake: Omitting the name attribute on a control

A field without a `name` attribute contributes nothing to the submission. The browser encodes every named control in the form, in source order, and silently ignores the rest. There is no console warning, no error, and no empty key in the request. The server receives a payload that looks correct except for the missing field, making this one of the harder client-side bugs to spot. Always verify the actual network request in DevTools when a form value is not reaching the server.

## Tip: Use GET for searches and filters, POST for everything that changes state

The HTTP specification defines GET as a safe, idempotent method: calling it repeatedly has no side effects. POST is the opposite: it is intended for operations that change something. Following this convention earns you browser caching, user-shareable URLs, and safe reload behaviour for free. It also means your URLs become meaningful: `/products?category=shoes&sort=price` is a real resource a user can bookmark and return to. A search form that uses POST loses all of this for no benefit.

## Try it: Try it

Switch between GET and POST and type a value to see how the submission changes: query string vs request body.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/form-and-submission

## Check yourself

### Question 1

A user submits a login form with username and password. Which method is correct and why?

- A. POST, because GET would expose the password in the URL
- B. GET, because login data needs to be bookmarkable
- C. Either method is fine; the difference is only about URL length

Correct answer: A

POST sends data in the request body, not the URL. This keeps passwords out of browser history, server logs, and referrer headers. GET is reserved for safe, idempotent operations like searches.

- Why not B: GET appends form data to the URL as a query string. A password in the URL appears in browser history, server logs, and over-the-shoulder visibility. POST sends the data in the request body and keeps it out of the URL.
- Why not C: URL length is one consideration, but the primary distinctions are idempotency, cacheability, and data visibility. Login is a state-changing operation with sensitive data, both of which point to POST.

### Question 2

A text input renders correctly but the server never receives its value. What is the most likely cause?

- A. The input is missing a name attribute
- B. The input is inside a <div> instead of directly inside <form>
- C. The input has type="text" instead of type="search"

Correct answer: A

Only controls with a `name` attribute are included in the form submission. A missing `name` means the browser silently skips the field, with no warning in the console or the network request.

- Why not B: Inputs can be nested inside any element within the form and still submit normally. The name attribute is what governs inclusion in the submission.
- Why not C: Both type values submit their value identically. The missing name attribute is the silent culprit: the browser encodes only named controls.

### Question 3

A form contains a file upload input. What enctype value is required?

- A. multipart/form-data
- B. application/x-www-form-urlencoded (the default)
- C. text/plain

Correct answer: A

`enctype="multipart/form-data"` is required whenever a form contains a file input. The default encoding cannot transmit binary file content, only the filename string.

- Why not B: The default encoding serialises file inputs as just the filename string with no file content. multipart/form-data encodes each field as a separate part and includes binary file data.
- Why not C: text/plain is a rarely-used encoding that does not handle binary data. multipart/form-data is the only standard encoding that transmits file content.

## Remember this

- GET appends data as a query string: use it for searches, filters, and any idempotent retrieval.
- POST sends data in the request body: use it for state changes, sensitive data, and large payloads.
- Controls without a `name` attribute are silently excluded from the submission.
- `enctype="multipart/form-data"` is required for file uploads; the default encoding only sends the filename.
