Understanding GET vs POST
The HTML <form> element uses the method attribute to specify how submitted form data is sent to the server.
The two methods most commonly used with HTML forms are get and post. Both can submit form data, but they send that data differently and are suited to different kinds of requests.
The method Attribute
The method attribute on the <form> element specifies the HTTP method used when the form is submitted.
<form action="/search.php" method="get">
...
</form>
<form action="/contact.php" method="post">
...
</form>
If the method attribute is omitted, the default method is get.
The GET Method
The get method sends submitted form data as part of the URL, usually in a query string that follows a question mark.
<form action="/search.php" method="get">
<label for="search">Search:</label>
<input type="search" id="search" name="q">
<button type="submit">Search</button>
</form>
GET is commonly used for requests that retrieve or filter information without intending to make a significant change on the server, such as searches, filters, and navigation-related forms.
GET Query Strings
With GET, successful form controls are encoded into the URL as name-and-value pairs.
https://www.example-web.site/search.php?q=html
In this example, q is the control's name and html is its submitted value.
Because GET data becomes part of the URL, the resulting address can often be bookmarked, copied, shared, and revisited with the same parameters.
The POST Method
The post method sends submitted form data in the body of the HTTP request rather than placing it in the URL query string.
<form action="/contact.php" method="post">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<button type="submit">Submit</button>
</form>
POST is commonly used when submitted data creates or changes something on the server, sends longer form data, or includes information that should not appear in the URL.
GET vs POST Comparison
| Feature | GET | POST |
|---|---|---|
| Where data is sent | URL query string | Request body |
| Visible in address bar | Yes | No |
| Can commonly be bookmarked | Yes | No |
| Typical use | Searches and data retrieval | Submitting or changing data |
| File uploads | No | Yes, with multipart encoding |
Security Considerations
POST does not make submitted data automatically secure. It keeps form values out of the visible URL, but the request data still needs protection while it travels between the browser and server.
Forms that handle sensitive information should be served over HTTPS, and all submitted values should be validated and processed safely on the server.
Sensitive information such as passwords should not be submitted with GET because GET values can appear in URLs, browser history, logs, bookmarks, and copied links.
Choosing GET or POST
Use GET when the form is primarily requesting information and the submitted parameters can reasonably appear in the URL.
<form action="/search.php" method="get">
Use POST when the form submits data that changes server state, contains information that should not appear in the URL, sends a larger body of information, or uploads files.
<form action="/contact.php" method="post">
The method should reflect what the request is intended to do rather than simply how many fields the form contains.
GET and POST Example
The following example contains two small forms. Submit the GET form and observe how its value appears in the URL, then submit the POST form and compare the result.
GET and POST Best Practices
- Use GET for searches, filters, and other requests that primarily retrieve information.
- Use POST when submitting information that creates or changes data on the server.
- Do not submit sensitive values with GET.
- Use POST with
enctype="multipart/form-data"when uploading files. - Use HTTPS whenever form data needs protection while being transmitted.
- Validate and process all submitted data safely on the server regardless of the method used.
- Choose the method based on the purpose of the request, not simply the number of form fields.
Summary
GET and POST are the two methods most commonly used to submit HTML forms. GET places form data in the URL query string and is well suited to searches and information retrieval, while POST places form data in the request body and is commonly used when submitting or changing information.
Neither method replaces HTTPS or server-side validation. Choosing the appropriate method helps make forms behave predictably and match the purpose of the request.
