Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Frontend Basics for Backend Developers  ›  Lesson

HTML Forms — How Pages Send Data

HTML 12 minAn HTML form turns its named fields into an HTTP request, using its action as the URL and its method as GET with a query string or POST with a body.

HTML Forms — How Pages Send Data

A shopper types "poems" into the bookshop's search box and presses Enter. No JavaScript runs, yet your server receives a request with that word in it. The HTML form did it, and knowing exactly what a form sends lets you read your server's input without guessing.

The rule

action is the URL, method is the verb, name is the key.

<form action="/search" method="get">
  <input type="search" name="q">
  <button>Search</button>
</form>
  • action is where the request goes. If you leave it out, the form submits to the current page's URL.
  • method is the HTTP method: get or post. Plain HTML forms can send only these two; if you omit it, the default is get.
  • name on each field becomes the key. The field's current value becomes the value. A field without a name is not sent at all.

With method="get", the fields go into the URL as a query string. Submitting "poems" requests:

GET /search?q=poems HTTP/1.1

Spaces and special characters are encoded, so "salt road" becomes q=salt+road. Because the values sit in the URL, they also end up in browser history, bookmarks and server logs, which is one more reason never to send a password with GET.

With method="post", the same key=value pairs travel in the request body instead, by default with Content-Type: application/x-www-form-urlencoded:

<form action="/newsletter" method="post">
  <input type="email" name="email" required>
  <input type="text" name="name">
  <button>Subscribe</button>
</form>
POST /newsletter HTTP/1.1
Content-Type: application/x-www-form-urlencoded

email=asha%40mail.example&name=Asha

(%40 is the encoded @.) Attributes like required and type="email" make the browser check the input first, but that is only a courtesy to honest users. Anyone can send the request without the form, so the server must check again.

The shortcut

Rebuild the request in your head from three attributes: take method, append action, then write name=value for every named field, in the URL for GET or in the body for POST. Then ask whether the method fits: reading uses GET, changing something uses POST.

What does the newsletter form send?Step 1 of 4
  1. Method and action

    method="post", action="/newsletter": the request line is POST /newsletter.

Drills

Answer fast — trust the rule.

quick drill

A GET form with action="/search" has one field name="q". The user types poems. What is requested?

quick drill

A text input has no name attribute. What does the server receive for it?

quick drill

Where do the fields of a method="post" form travel?

quick drill

The form uses required on the email field. Can the server skip checking that email is present?

Remember

  • action is the URL, method is the verb, name is the key.
  • GET puts name=value in the query string; POST puts it in the body; unnamed fields are skipped.
  • Browser validation is convenience; the server still checks everything.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFrom form to request

The bookshop has a search form (one field called q) and a newsletter form (fields email and name). Explain which method each form should use, and describe the exact request the browser sends for each when submitted.