Form submission, methods and encoding
Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.
What you will be able to do
#- Choose between GET and POST for a given form.
- Explain how form data is encoded and submitted.
Overview
#A form has an action, the URL that receives the data, and a method, either GET or POST. GET appends the data to the URL as a query string, so it is visible, bookmarkable and length-limited. POST sends the data in the request body, so it is not shown in the URL and has no practical length limit.
Use GET for searches and filters, where a shareable URL is a feature. Use POST for anything that creates, changes or deletes data, and for anything sensitive such as credentials. A login form submitted with GET puts the password in the URL, browser history and server logs.
<form action="/search" method="get">
<label for="q">Search</label>
<input id="q" name="q" type="search" />
<button>Search</button>
</form>
<form action="/login" method="post">
<label for="password">Password</label>
<input id="password" name="password" type="password" />
<button>Sign in</button>
</form>
The default encoding for POST is application/x-www-form-urlencoded. When a form includes file uploads, set enctype="multipart/form-data" or the file will not be sent. GET always uses the URL query encoding regardless of enctype.
Each successful control contributes its name and value. Unchecked checkboxes and radio buttons are not submitted at all, which is why server code must treat a missing checkbox as false rather than assuming it was sent.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
A form with method="get" and one input named q is submitted with the value 'css grid'. What appears in the address bar?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.html<form action="/search" method="get"> <input name="q" value="css grid" /> </form>Exercise 2Debug
This upload form never sends the selected file. Fix it.
starter.html<form action="/upload" method="post"> <label for="file">Resume</label> <input id="file" name="file" type="file" /> <button>Upload</button> </form>Reveal the reference answer
reference.html<form action="/upload" method="post" enctype="multipart/form-data"> <label for="file">Resume</label> <input id="file" name="file" type="file" /> <button>Upload</button> </form>Authored expected output: The form declares enctype="multipart/form-data" so the file is included in the request body.
File inputs require multipart encoding; the default urlencoded encoding cannot carry binary file data.
Common misconceptions
#- GET and POST are interchangeable as long as the server handles both.
- Unchecked checkboxes submit an empty value.
Follow-up practice
#- html-css-10-error-messaging
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.