The method attribute of the HTML form specifies how form data is sent to the server.
HTML forms mainly use GET and POST methods. GET is used for fetching data, while POST is used for submitting data securely to the server.
Types of Form Methods
1. GET Method
- Sends form data through the URL.
- Data is visible in the browser's address bar.
- Suitable for search forms and non-sensitive data.
Example:
<form action="process.php" method="get">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>
2. POST Method
- Sends form data in the HTTP request body.
- Data is not visible in the URL.
- Suitable for login forms, registration forms, and sensitive information.
Example:
<form action="process.php" method="post">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>
Difference Between GET and POST
| GET | POST |
|---|---|
| Data is visible in URL | Data is hidden from URL |
| Limited data size | Can send large amounts of data |
| Used for retrieving data | Used for submitting data |
| Less secure | More secure |