Forms let visitors talk back to your site: place an order, send a message, log in. In this section we build forms for the Brew and Byte cafe. First, the foundations.
The form element#
Everything lives inside a <form>. Its two most important attributes decide where the data goes and how it travels.
<form action="/subscribe" method="post">
<label for="email">Email address</label>
<input type="email" id="email" name="email">
<button type="submit">Subscribe</button>
</form>actionis the URL that will receive the data when the form is submitted. If you omit it, the form submits to the current page.methodis the HTTP method. The two you need aregetandpost.
GET versus POST#
| GET | POST | |
|---|---|---|
| Where data goes | In the URL, as a query string | In the body of the request |
| Visible to the user | Yes | No (but not secret!) |
| Bookmarkable | Yes | No |
| Good for | Searching, filtering | Creating or changing data, logins, long text |
A search box uses GET, so the results page can be shared as a link. A sign-up or order form uses POST. Neither method is encryption. Protect real secrets by serving your site over HTTPS.
The name attribute#
When the form is submitted, the browser sends each field as a name and value pair. A field without a name is not sent at all. The name is the key your server code will look up.
<form action="/search" method="get">
<input type="search" name="q" id="q" aria-label="Search the menu">
<button>Search</button>
</form>Typing "mocha" and submitting takes you to an address ending in /search?q=mocha.
What submitting does#
A form is submitted when the user presses a submit button or presses Enter inside a text field. The browser then:
- Checks built-in validation rules (covered in a later lesson).
- Collects every named field's value.
- Sends the request to
actionwith the chosenmethod. - Displays the server's response.
You will not process data on the server in this tutorial (that is the job of a back-end language), but you can see everything the browser sends using Developer Tools, in the Network tab.
Other useful form attributes#
autocomplete="on"or"off"hints whether the browser may suggest saved values. Individual fields can use specific tokens likeautocomplete="email"to help password managers and autofill.novalidateturns off the browser's built-in validation.enctype="multipart/form-data"is required when a form uploads files.target="_blank"shows the response in a new tab.
<!-- a file upload form needs POST and the multipart encoding -->
<form action="/upload" method="post" enctype="multipart/form-data">
<label for="photo">Your latte art photo</label>
<input type="file" id="photo" name="photo" accept="image/*">
<button type="submit">Upload</button>
</form>Common mistakes#
- Forgetting the
nameattribute, then wondering why the server received nothing. - Using GET for passwords or large messages.
- Omitting a submit button, leaving keyboard users with no obvious way to send.
- Assuming POST makes data private.
- Nesting one form inside another, which is not allowed.
Try it yourself#
Create a search form using method="get" with an input named q and a button. Submit it and read the address bar. Then create a newsletter form using method="post" and open the Network tab in Developer Tools to inspect the request. Remove the name attribute from one field and compare what is sent.
Recap#
A form has an action (where) and a method (how). Only fields with a name are submitted. Use GET for searches, POST for changes, and always validate on the server as well.
Check your understanding
Lesson quiz
OptionalA short quiz to check what you just learned.
- Questions
- 4
- Time limit
- 5 min
- Pass mark
- 70%
- Best score
- -