GET vs POST: which should my form use?
Short answer
Use GET when the request only reads data and the URL should be shareable. Use POST when the request changes something, or carries anything you would not want in a browser history or server log.
| GET | POST | |
|---|---|---|
| Where the data goes | The URL | The request body |
| Bookmarkable | Yes | No |
| Size limit | ~2000 characters | Effectively none |
| Safe to repeat | Yes | No |
| Read in PHP from | $_GET | $_POST |
#Use GET for
Search boxes, filters, pagination, sorting. You want /search?q=python&page=2 to be shareable and bookmarkable.
#Use POST for
Logins, sign-ups, comments, purchases, deletions. Anything that changes server state.
#Detecting which you got
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// handle the submission
}Check the method, not isset($_POST['field']). An unchecked checkbox is not sent at all, so a field check can wrongly conclude nothing was submitted.
#Redirect after POST
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !$errors) {
header('Location: /thanks.php');
exit;
}Without this, refreshing the result page resubmits the form — which is how one message becomes five. The pattern is called Post/Redirect/Get.