Forms: GET or POST?
A form lets a visitor send data to the server. The form tag has two main attributes. action names the page that receives the data, for example add_term.php. method says how the data travel. With get, the data are added to the address, for example search_term.php?term=router. This is good for a search, because the address can be saved and shared, but never for anything private. With post, the data are sent inside the request and do not appear in the address; it suits adding or changing data. But post is not a secret by itself: without a protected connection the data can still be read on the way. Limits on how much can be sent are set by the browser and the server, so do not rely on a number. A form that sends a picture file also needs enctype="multipart/form-data". For scale, a server might allow an upload of at most 2 MB (we use 1 MB = 1,024 KB, and 1 KB = 1,024 bytes), but the real limit is the server’s setting.
| GET | POST | |
|---|---|---|
| Where do the data go? | into the address | inside the request, not shown |
| Good for | searching; pages you may save | adding or changing data |
| For passwords? | never | only with a protected connection |
| Size limit | set by browser and server | set by server settings |
1<form method="get" action="search_term.php"> 2<input name="term" type="text"> 3<input type="submit" value="Search"> 4</form>
The page shows a text box and a button labelled Search. After a click with the word router, the address ends with search_term.php?term=router.
| Need | Method | Reason |
|---|---|---|
| search for a term | GET | the address can be saved and shared |
| add a new term | POST | it changes data and has long text |
| sign in with a password | POST, with a protected connection | never in the address, and still not secret by itself |
On paper make a table of four forms on a school website (search the library, send a message to the club, enter a mark, ask for a password reminder). Choose GET or POST for each and write a one-line reason. Mark which one needs enctype for a file.