method
The HTML method attribute defines how form data is sent to a server, using either GET for visible data or POST for hidden data.
method attribute
The HTML method
attribute defines how to send form data to a server within a <form>
element, specifying the HTTP
method for submission. It can take two values:
Syntax
index.html
<form method="GET | POST">
Example
index.html
<form method="get">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="firstname" placeholder="Your name..">
<br><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lastname" placeholder="Your last name..">
<br><br>
<input type="submit" value="Submit">
</form>
Definition and Usage
The method
attribute defines how to send form data to the page specified in the action
attribute.
Form data can be sent as URL variables using method="get"
, or as an HTTP POST request with method="post"
.
GET
adds form data to the URL, with size limits and visible information, making it ideal for non-sensitive data and bookmarking, whereas POST
includes data in the request body, with no size limits, but cannot be bookmarked and keeps data hidden from the URL.Values
- GET: This method sends data as part of the URL, making it visible. It's not recommended for transmitting sensitive information such as passwords or financial details.
- POST: This method sends data in the body of the
HTTP
request, keeping it hidden. It is more secure for transmitting sensitive information.
Applies To
The method
attribute can be applied to the following HTML elements.
Conclusion
The method
attribute in HTML determines how form data is transmitted to the server, either through GET
or POST
. GET
appends data as visible URL parameters, while POST
securely sends it in the request body. The choice between them depends on the data type, particularly when sensitive information needs protection.