What does the method attribute do on <form>?
The method attribute on the <form> tag determines how the data is sent from the form to the server, that is, exactly how the browser will transmit the information entered by the user.
It sets the request's HTTP method and directly affects where the data will be located: in the address bar or in the request body.
1. Syntax
<form action="/submit" method="post">
<input type="text" name="username">
<button type="submit">Submit</button>
</form>Here:
action="/submit": where we send the data;method="post": how we send it.
2. Two main values of the method attribute
| Method | How it works | Where the data is visible | Where it is used |
|---|---|---|---|
| GET | Data is appended to the URL (in the browser's address bar) | In the address (for example, ?name=Oleh) | Search, filters, simple forms |
| POST | Data is sent "in the body" of the HTTP request | Invisible to the user | Registration, login, forms with passwords |
3. Example of the GET method
<form action="/search" method="get">
<input type="text" name="q" placeholder="Search...">
<button type="submit">Search</button>
</form>If the user enters phone, the browser sends this request:
/search?q=phoneFeatures:
- the data is visible in the address bar;
- easy to copy and share as a link;
- cannot be used to pass confidential data (passwords, cards, etc.);
- limited by the URL length (about 2000 characters).
4. Example of the POST method
<form action="/register" method="post">
<input type="text" name="login">
<input type="password" name="password">
<button type="submit">Sign up</button>
</form>The browser will send the data in the body of the HTTP request, not in the address bar:
POST /register
Content-Type: application/x-www-form-urlencoded
login=Oleh&password=12345Features:
- the data is not visible to the user;
- no length limits;
- suitable for sending personal or large amounts of information (files, registration forms, orders, etc.).
5. Other methods (rarely used)
Although HTML forms only support get and post,
modern web applications (via JavaScript / API) also use other HTTP methods:
PUT: updates data;DELETE: deletes data;PATCH: partially updates data.
But they work only through JavaScript or API requests, not directly in HTML.
Summary:
| Parameter | Value |
|---|---|
| Attribute | method |
| Purpose | Determines how the form's data is transmitted |
| Main values | get and post |
GET | Adds data to the URL, suitable for search |
POST | Passes data in the request body, suitable for forms with personal information |
| If the attribute is not set | GET is used by default |
In simple terms:
method is "how we send the letter":
GET: like a postcard, the text is visible to everyone;POST: like an envelope, the data is inside, no one sees it.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.