HTML5 Intermediate Accessible Forms · 9 min read

HTML Form Methods: GET versus POST

Lesson 14 of 36 in the HTML5 learning path.

When should a form use GET instead of POST?

Form method chooses where the fields go and whether repeating the request is safe. GET appends the names and values to the URL. Use it for search and filters. The result can be bookmarked, shared, and fetched again, and fetching it again does not change the catalog. That repeatability is idempotency. POST puts the fields in the request body. Use it when the form creates or updates a record, and whenever a password is collected, because a query string is stored in history and logs and can leak through a referrer.

The enroll form is POST. It sends the school email, the password, and a hidden _token that the server rendered for this session. The server accepts the POST only when that token matches the one it expects. Keep the token in the body. The search form above it stays GET, with the public word tables in the query, so a classmate can bookmark the result.

Syntax & Parameters

Syntax
<form method="get" action="/courses"></form> <form method="post" action="/enroll"></form>

Example

Interactive preview available
search.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Search courses or enroll</title>
  <style>
    body { margin: 0; font: 1.05rem/1.55 Georgia, "Times New Roman", serif; color: #1c1917; background: #faf7f2; }
    header { background: #1e3a5f; color: #f8fafc; padding: 1.25rem 1.5rem; }
    main { max-width: 42rem; margin: 0 auto; padding: 1.25rem 1.5rem 2rem; }
    h1 { font-size: 2rem; line-height: 1.2; margin: 0 0 0.75rem; }
    h2 { font-size: 1.25rem; margin: 1.4rem 0 0.45rem; }
    p { margin: 0 0 0.85rem; }
    label { display: block; font-weight: 600; margin: 0.8rem 0 0.3rem; }
    input { width: 100%; max-width: 24rem; box-sizing: border-box; padding: 0.45rem 0.55rem; font: inherit; }
    button { font: inherit; margin-top: 0.8rem; padding: 0.4rem 0.75rem; }
    form { margin-bottom: 1.25rem; }
  </style>
</head>
<body>
  <header>
    <p>North Campus, Web Systems 1</p>
  </header>
  <main>
    <h1>Search courses or enroll</h1>
    <h2>Search the catalog</h2>
    <p>Search uses GET. The words belong in the URL so a result can be bookmarked and opened again. Repeating that request does not enroll anyone.</p>
    <form method="get" action="/courses">
      <label for="q">Search courses</label>
      <input id="q" name="q" type="search" value="tables">
      <button type="submit">Search</button>
    </form>
    <h2>Enroll in Web Systems 1</h2>
    <p>Enrollment uses POST. The password stays in the body, not the address bar. The hidden token was rendered for this session. The server accepts the POST only when that token matches.</p>
    <form method="post" action="/enroll">
      <input type="hidden" name="_token" value="sess_8f3c2a91e6b04d17">
      <label for="student-email">School email</label>
      <input id="student-email" name="email" type="email" autocomplete="email" required>
      <label for="portal-password">Portal password</label>
      <input id="portal-password" name="password" type="password" autocomplete="current-password" minlength="12" required>
      <button type="submit">Enroll</button>
    </form>
  </main>
</body>
</html>

Expected Output

Choosing Search requests GET /courses?q=tables. The address bar shows that URL, so it can be bookmarked and opened again. Refreshing the result loads the same URL and does not ask to resubmit, because a GET is fetched, not replayed as a change. The response body would be the course list for the word tables.

Choosing Enroll sends POST /enroll. The body contains email, password, and _token. The password does not appear in the address bar. The browser may warn before resending that POST on refresh, because the request is not a safe repeat. If the token does not match the session, a correct server responds with a rejection and does not create the enrollment. A matching token continues into the normal enroll handler.

Line-by-Line Walkthrough

  1. The search form sets method="get" and action="/courses". Submitting it requests that path with a query string, not a body.
  2. The search field is type="search" with name="q" and an initial value of tables, so the sample query is visible and public.
  3. Repeating that GET is safe. It does not enroll the student. A search is idempotent: the same request again returns the same kind of result and does not create a second enrollment.
  4. The enroll form sets method="post" and action="/enroll". The fields travel in the request body, and the address bar stays on /enroll.
  5. The password uses type="password" and autocomplete="current-password". It is on the POST form only, because a password must not be placed in a query string.
  6. The hidden input named _token carries sess_8f3c2a91e6b04d17, a token the server rendered for this page. It is not the password and not a second secret typed by the student.
  7. The server is expected to compare that token with the value stored for the session and to reject a POST that arrives without a match. That is the CSRF check, described here at the level of the form, not as an attack.
  8. The token sits in the body with the other POST fields. Putting it in the URL would leak it into history and logs the same way a password would.
  9. Both forms have explicit submit buttons. Search does not include the email or the password, so those values cannot ride along in the query string.

Submit Search and read the address bar. Submit Enroll only on a server that expects this POST. The password and the token belong in the body, and a refresh after POST can ask before sending the enrollment again.

Common Pitfalls & Security

GET is for reading a result you are willing to share. POST is for a change, a password, or a token. Mixing them up stores secrets in places you cannot easily delete, or lets a repeat of a harmless-looking URL create a second record.

  • A password in a GET query lands in browser history, server logs, and possibly the Referer header of the next request.
  • Bookmarking a POST result does not bookmark the body. People bookmark the URL and lose the fields, or worse, you moved the password into that URL to make the bookmark work.
  • A search implemented as POST cannot be shared as a link and will warn on refresh even though no data changed.
  • The CSRF token must travel in the POST body and must be checked by the server. A hidden input alone does nothing if the handler ignores it.
  • Caching a POST response, or letting a form stay method="get" after you add a password field, exposes the new field the next time someone submits.

Best Practices & Modern Standards

HTML5 forms should use GET when the action is a query and POST when the action changes data. Keep search parameters short and non-sensitive so the query string stays a reasonable URL. Keep passwords, uploaded files, and session tokens out of that string.

Render the CSRF token into the POST form for the current session and compare it on the server before you insert or update a row. Do not invent extra secrets in the HTML beyond that token. Label both forms, use the right input types, and make the submit buttons say Search and Enroll so the method and the verb agree.

Real-World Use

The course finder at /courses is the GET form. A student searches for tables, copies the URL /courses?q=tables into the class chat, and every classmate opens the same result. Refreshing that page is safe because it only reads the catalog.

The Enroll button on the same screen is the POST form. It sends the school email, the portal password, and the session token to /enroll. The password never shows in the address bar. The registrar system stores an enrollment only when the token matches. A duplicate refresh is the browser warning about resubmitting, which is the right friction for a request that creates a seat.

Frequently Asked Questions

What does idempotent mean for a search form?

It means repeating the request does not change the stored data. GET /courses?q=tables can be fetched any number of times and the catalog stays as it was. Enroll is not idempotent, because a second successful POST could try to create another seat. That is why enroll is POST and search is GET.

Can I bookmark a POST form?

You can bookmark the page that contains the form, but the bookmark does not store the POST body. If the result must be shareable, the form should have been GET and the parameters should be safe to put in the URL. Do not move a password into the URL just to make a bookmark work.

Where does the CSRF token belong?

It belongs in the POST body, usually as a hidden input the server rendered for this session. The server compares it with the value it stored for that session and rejects a mismatch. It is not a substitute for a password, and it should not be placed in the query string.

Why does refresh warn after enroll but not after search?

A refresh of a GET loads the URL again, and that is an ordinary fetch. A refresh of a POST would send the body again, so browsers ask before repeating a request that may create or change a record. The warning is a signal that the method is doing more than a search.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…