HTML Form Action POST and GET

HTML forms provide two methods of submitting form data to the server: POST and GET. In this article, we will discuss the differences between POST and GET and provide examples of how to use each method in HTML.

POST method

The POST method sends the form data to the server as part of the HTTP request body. This means that the data is not visible in the URL, which is useful for submitting sensitive information such as passwords.

To use the POST method, set the "method" attribute of the

tag to "post", like this:

1<form method="post" action="submit-form.php"> 2 <!-- form elements go here --> 3</form>

When the user submits the form, the data is sent to the server using the POST method. In the example above, the data is sent to "submit-form.php", which is the URL of the server-side script that will process the form data.

GET method

The GET method sends the form data to the server as part of the URL query string. This means that the data is visible in the URL, which can be useful for bookmarking or sharing the URL with others.

To use the GET method, set the "method" attribute of the tag to "get", like this:

1<form method="get" action="submit-form.php"> 2 <!-- form elements go here --> 3</form>

When the user submits the form, the data is sent to the server using the GET method. In the example above, the data is sent to "submit-form.php", which is the URL of the server-side script that will process the form data.

Differences between POST and GET

The main differences between POST and GET are as follows:

  • Data visibility: POST data is not visible in the URL, while GET data is visible in the URL.
  • Data length: POST data can be much longer than GET data, because it is sent as part of the HTTP request body rather than the URL query string.
  • Security: POST data is more secure than GET data, because it is not visible in the URL.

In general, you should use the POST method when submitting sensitive information, and the GET method when submitting non-sensitive information or when you want to share the URL with others.

Conclusion

HTML forms provide two methods for submitting form data to the server: POST and GET. The POST method sends the data as part of the HTTP request body and is more secure, while the GET method sends the data as part of the URL query string and is useful for bookmarking or sharing the URL with others.