Back to HTML Notes
Topic #179

form method

Understand how the method attribute controls data transmission in HTML forms, specifically distinguishing between GET and POST requests.

What it is

The method attribute on an HTML <form> element specifies the HTTP method used to submit form data. The two primary values are get and post. This attribute determines how user input is packaged and sent to the server. Related terms include query strings (used by GET) and request bodies (used by POST).

Why it matters

  • Data Visibility: GET exposes data in the URL, making it visible in browser history and logs, while POST hides data in the request body.
  • Security: POST is preferred for sensitive information like passwords because data does not appear in the address bar.
  • Caching: GET requests can be cached by browsers and servers; POST requests generally cannot.
  • Length Limits: URLs have length limits, restricting GET data size, whereas POST has no practical limit on data volume.
  • Idempotency: GET should only retrieve data without side effects, while POST is intended for operations that change server state.

Syntax or steps

Add the method attribute inside the opening <form> tag. Set its value to either "get" or "post". If omitted, the default is get.

<form action="/submit" method="post">
  <!-- form fields -->
</form>

Example

Below is a minimal login form using the POST method to securely transmit credentials.

<!DOCTYPE html>
<html>
<body>
  <form action="/login" method="post">
    <label for="username">Username:</label>
    <input type="text" id="username" name="username"><br><br>
    
    <label for="password">Password:</label>
    <input type="password" id="password" name="password"><br><br>
    
    <input type="submit" value="Login">
  </form>
</body>
</html>

Explanation: The action="/login" defines where data goes. The method="post" ensures data is sent in the body. Input fields with name attributes ("username", "password") are paired with their values and transmitted upon submission.

Common mistakes

  • Using GET for passwords: Never use method="get" for sensitive data. It appears in the URL, exposing it to shoulder surfing and browser history.
  • Forgetting the name attribute: Inputs without a name attribute are ignored during submission, regardless of the method used.
  • Misunderstanding caching: Developers often expect POST results to be cacheable like GET pages. They are not, which affects performance strategies.
  • Assuming POST is inherently secure: POST hides data from the URL but does not encrypt it. Always use HTTPS alongside POST for security.

When to use it

Choose the method based on the intent of the operation.

FeatureGETPOST
Data LocationURL Query StringRequest Body
VisibilityVisible in Address BarHidden from Address Bar
Use CaseSearch, Filtering, RetrievalLogin, Uploads, Creating Records
BookmarkableYesNo

Practice

Guided Exercise: Create a simple search form that uses the GET method. Ensure the input field has the name q. Submitting this form should append ?q=your_search_term to the URL.

Challenge: Modify the previous form to use POST instead. Observe how the URL remains unchanged after submission, assuming the server handles the redirect correctly.

Quick check

Question: Why is POST preferred over GET for submitting large amounts of text?

Answer: Because GET appends data to the URL, which has strict length limitations imposed by browsers and servers. POST sends data in the request body, allowing for much larger payloads.

Summary

The method attribute dictates how form data travels to the server. Use GET for retrieving data via visible, bookmarkable URLs, and use POST for sending hidden, potentially large, or sensitive data that modifies server state.

Want to go beyond the notes?

Join Coding Now Tech Institute's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

form method – FAQs

Quick answers about learning form method in HTML.

This free note from Coding Now Tech Institute explains form method in HTML — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every HTML topic on Coding Now Tech Institute, including form method, is 100% free with no signup required.
With focused practice, most students grasp form method in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now