Sub topic 1
Anatomy of a form: form, action, and method
Prerequisites
This chapter assumes you know basic HTML document structure: elements, attributes, and nesting. You should also be able to open a page in a browser and use its developer tools. No JavaScript is needed here.
What a form does
A <form> element groups controls (inputs, buttons, menus) and tells the browser where and how to send their values. When the user submits the form, the browser collects the values of the controls inside it, builds a request, and sends it to a server. The browser then loads the response as a new page.
Two attributes control that request:
actionis the URL that receives the data.methodis the HTTP method used to send it. The two values you will use aregetandpost.
A minimal form
<form action="/subscribe" method="post">
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required>
<button type="submit">Subscribe</button>
</form>The <form> element holds three things here: a label, an input, and a submit button. When the user clicks the button, the browser sends a POST request to /subscribe. The body contains email= followed by the typed address.
Later chapters cover the label, the input attributes, and the button in detail. For now, focus on the form element itself.
The action attribute
action accepts an absolute URL, a path, or a relative URL. A relative value is resolved against the page's address.
If you omit action, the form submits to the URL of the current page. This is valid, but an explicit action makes the form easier to read and maintain.
The method attribute
If you omit method, the default is get. Many forms are written without it, so check for it when you debug.
| Method | Where the data goes | Typical use |
|---|---|---|
| `get` | In the URL query string | Searches and filters, where the URL should be shareable |
| `post` | In the request body | Creating or changing data, such as sign-ups and orders |
A get form with a field named q and the value forms requests /search?q=forms. A later chapter compares the two methods in detail.
What happens on submit
Note the validation step. Built-in validation runs before the request is built, so an invalid form never reaches the server.
Common errors
- A control has no
name. The browser leaves it out of the submitted data. This is the most frequent reason a value "disappears". - The control is outside the form. Only controls inside the
<form>element, or linked to it with theformattribute, are submitted. - Forms are nested. A
<form>inside another<form>is invalid HTML, and the browser ignores the inner tag. - Sensitive data uses
get. Query strings appear in browser history, server logs, and shared links. Usepostfor passwords and personal data.
Try it
Save the minimal form above in a file and open it in a browser. Open the developer tools, switch to the Network tab, and submit the form with a valid address. Look at the request method and the request payload. Then change method="post" to method="get" and submit again. The address now appears in the URL.
Exercise
A form has action="/search" and no method attribute. It contains <input name="q" type="text">, and the user types "forms" and submits. Which request does the browser send?
Show answer
A GET request to /search?q=forms. With no method, the default is get, so the value goes in the query string.
Exercise
A form contains <input id="city" type="text"> with no name. The user types "Berlin" and submits. What does the server receive for this field?
Show answer
Nothing. Controls without a name attribute are not included in the submitted data.