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.
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.
Try it
Switch between GET and POST and type a value to see how the submission changes: query string vs request body.
<form method="GET" action="/search"> <input name="query" type="text" /> <button type="submit" > Search </button></form>
HTTP method
Form value
Submission: data in the URL
URL
example.com/search?query=html+tables
Request body
none
Bookmarkable. Appears in browser history and server logs. Safe to cache. Limit ~2 000 chars.
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
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
nameattribute are silently excluded from the submission. enctype="multipart/form-data"is required for file uploads; the default encoding only sends the filename.
Done with this concept?
Mark it complete to track your progress. No login needed.