Skip to the workspace
Pebellslearn by building
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.

backend / HTML Forms — How Pages Send Data

Explore each scene at your own pace.

01 · The rule

The rule

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.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.