Form submission: GET vs POST & file uploads
5 min lesson
- When a form is submitted, the browser sends the field names and values to the server using a method: GET or POST.
- GET puts the data in the URL, which is visible and can be bookmarked but is not private.
- POST puts the data in the request body, which is better for private or large data.
- Think of GET as writing your order on a postcard and POST as sealing it in an envelope.
- To upload files, use a file input and set enctype="multipart/form-data" on the form.
- This builds on Form validation & types: the checked data now travels to the server.
- The example shows a search form using GET and an upload form using POST with multipart.
- Next, Interactive elements: details, summary & dialog adds built-in interactive widgets.
- Use GET for searches and filters that should be shareable.
- Use POST for passwords, uploads and anything private or large.
- Add enctype="multipart/form-data" for any form with a file input.
- Never send passwords or personal data with GET.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form submission</title>
</head>
<body>
<!-- GET: the search term appears in the URL -->
<form action="/search" method="get">
<label for="q">Search</label>
<input type="text" id="q" name="q">
<button type="submit">Search</button>
</form>
<!-- POST with multipart: needed for file uploads -->
<form action="/upload" method="post" enctype="multipart/form-data">
<label for="file">Choose a file</label>
<input type="file" id="file" name="file">
<button type="submit">Upload</button>
</form>
</body>
</html>What happens: A page with a "Search" field and button, and a "Choose a file" field with an "Upload" button. Submitting the search puts ?q=... in the URL; the upload form sends the chosen file in the request body.
Breaking it down
method=get
<form method="get">Appends the field names and values to the URL as a query string. Good for searches, bad for secrets.
method=post
<form method="post">Sends the data in the request body. Better for private data, large data and changes to the server.
enctype multipart
enctype="multipart/form-data"Required for file uploads. Without it, the server cannot read the file.
file input
<input type="file" name="file">Opens a file picker. The name is the key the server uses to find the uploaded file.
GET vs POST
| Feature | GET | POST |
|---|---|---|
| Where data goes | In the URL | In the request body |
| Visible to user | Yes | No |
| Good for | Search, filters | Passwords, uploads |
| Can be bookmarked | Yes | No |
A helpful analogy
GET is a postcard: anyone handling it can read the address and message. POST is a sealed envelope: only the receiver opens it. File uploads need the multipart envelope because a photo cannot fit on a postcard.
Nesting rules
- 1.Set enctype on the form, not on the input.
- 2.Use only one method per form; a form cannot be both GET and POST.
Common mistakes
Sending a password with GET
Avoid
<form method="get"><input type="password">Use instead
<form method="post"><input type="password">GET puts the password in the URL, which appears in history and logs. Use POST for anything private.
Forgetting multipart on a file upload
Avoid
<form method="post"><input type="file">Use instead
<form method="post" enctype="multipart/form-data">Without multipart, the server receives the file name instead of the file contents.
Quick tips
- •Watch the URL after submitting a GET form; the field names and values appear in it.
- •Open the Network panel and inspect a POST request to see the body the server actually received.