Send Data With a Form

The garden’s work day page tells volunteers to sign up at an address on another site, and that address was never real. Rosa wants the sign-ups to start on the garden’s own page, which means the page has to collect what visitors type.

In this lesson, you’ll add a first sign-up form to the work day page, submit it, and read exactly what the browser sends by checking the address bar. Start by getting the site in front of you.

Set Up the Starter Site

During this course, you’ll build forms every visitor can fill in, then check the whole site for accessibility with the keyboard, DevTools, and a screen reader, all in plain HTML and CSS. Each section ends with exercises, and you’ll build Rosa’s plot application form in a project.

The starter is the garden’s site as the responsive course left it, with its pages, nav, and layered stylesheet, so your pages match the screenshots. Skip any step you’ve already done:

  1. Get a current version of Chrome or Edge.
  2. Install VS Code, then install Live Preview by Microsoft from its Extensions view, which you open with Ctrl+Shift+X, or Cmd+Shift+X on macOS.
  3. Download and unzip the course’s sample code ZIP file, and copy its 01-send-form-data/garden/ folder to where you keep your projects. If you already have a garden/ folder there, rename yours first.
  4. Copy the six photos from garden-photos.zip, in the course’s supporting materials, into garden/images/.
  5. Open garden/ in VS Code using File → Open Folder, then open events/work-day.html.
  6. Open the Command Palette with Ctrl+Shift+P, or Cmd+Shift+P on macOS, and run Live Preview: Show Preview (External Browser). If another browser opens, paste its address into Chrome or Edge.

You’ll edit this garden/ folder directly, because the sign-up form is a lasting part of the site.

Submit a Form

A form gathers what a visitor types into its fields and sends it to an address you choose, so it first needs a page to send visitors to. In events/, copy work-day.html to signed-up.html. In the copy, change the <title> to Signed Up | Elm Street Community Garden and replace the <main> with this one:

Language: HTML
<main>
  <h1>Thanks for Signing Up</h1>
  <p>Your details aren't stored or passed to Rosa
    yet, because the garden's site has no program on
    a server to receive them. For now, the address
    bar shows what the form sent.</p>
</main>

Back in work-day.html, replace the paragraph that holds the fake link with <p>Rosa needs to know who's coming. Sign up below by Thursday.</p>. Then add this form right after the <div class="sign-up"> that holds that paragraph:

Language: HTML
<form action="signed-up.html" method="get">
  <p>Name <input name="name"></p>
  <p>Email <input name="email"></p>
  <p>Phone <input name="phone"></p>
  <button>Sign up</button>
</form>

Each part has one job:

  • action is the address the browser goes to when you submit the form, a relative path like the ones in your links.
  • method="get" puts the form’s data in that address, where you can read it. It’s also the default.
  • <input> is a void element with no closing tag, like <img>, that draws a text box.
  • name is the key the field’s value travels under.

The words before each box are plain text for now. They look like labels, but nothing ties them to their boxes yet. Save, and the work day page shows the form:

The work day page with a new form below Rosa's note, three text boxes after the words Name, Email and Phone, and a Sign up button.
The First Sign-Up Form on the Work Day Page

Type Priya Shah, [email protected], and 555 0187 into the three boxes, and click Sign up. The browser opens signed-up.html, and the address bar shows what the form sent:

The Thanks for Signing Up page, with an address ending in signed-up.html?name=Priya+Shah&email=priya%40example.com&phone=555+0187.
The Form's Pairs After the Question Mark

Everything after the ? is the form’s data: name=Priya+Shah&email=priya%40example.com&phone=555+0187. Each field becomes one name=value pair, and the pairs appear in field order, joined by &.

The browser encodes each name and value with the rules for form data, so a space becomes +, and @ becomes %40. It’s the same format as the old sign-up link’s ?event=workday&season=spring2027.

You can confirm that name decides what’s sent. Go back to the work day page with the browser’s Back button, and delete name="phone" from the phone box. Save, then fill in the form and submit again. The address now ends at &email=priya%40example.com, because the browser leaves out a field without a name. Here’s that trip from the fields to the address, with the phone field still unnamed:

Name and email become name=value pairs joined by & after signed-up.html?, and the phone field with no name is left out.
Only Named Fields Reach the Action Address

Put name="phone" back.

Your form really did send its data. What’s missing is a program at signed-up.html to store it. A real sign-up form submits to a program on a server or a form service, often with method="post", which sends the pairs inside the request instead of the address. Plain HTML can submit to one without JavaScript, but building that receiver requires more than HTML and CSS, so you’ll use get throughout.

If the trip from fields to address still feels fuzzy, open a chat with the AI mentor using the button below. Paste in the address you got, and it’ll go through it pair by pair:

Give Every Button a Type

Rosa also wants volunteers to see where the new compost bays will go before they sign up. You add a second button to the form, planning to make it open the plot map later:

Language: HTML
<button>Show plot map</button>
<button>Sign up</button>

As a programmer, you’d expect a button nobody wired up to do nothing. Save to reload the work day page. Type Priya Shah in the Name box, clear the Email and Phone boxes, and predict what clicking Show plot map will do. Then click it.

The browser goes to the Thanks for Signing Up page, exactly as Sign up would. Empty fields still send their names, with nothing after the =:

The Thanks for Signing Up page after clicking Show plot map, with an address ending in signed-up.html?name=Priya+Shah&email=&phone=.
Show Plot Map Submits the Form

To see why, go back to the work day page and open the Console. Type document.querySelector("form button").type, which reads the type of the first <button> inside the form. It shows 'submit'. Inside a form, a <button> with no type attribute is a submit button, so every button in the form sends it.

To fix it, set the type on every button. The one that sends the form gets type="submit", and any other gets type="button":

Language: HTML
<button type="button">Show plot map</button>
<button type="submit">Sign up</button>

Save, type Priya Shah again, and click Show plot map. This time, the address still ends in work-day.html:

The work day page still open after clicking Show plot map, with Priya Shah in the Name box and an address ending in events/work-day.html.
A Button With Type Button Doesn't Submit

The page stays put, but the button does nothing else, because making it show a map would take JavaScript. Going to the plot map is navigation, and navigation is a link’s job. Delete the Show plot map button, and add this paragraph above the form instead:

Language: HTML
<p>See where the compost bays go on the
  <a href="../plots.html">plot map</a>.</p>

Remember the rule: use a button for an action, a link for going somewhere, and a type on every button.

If you’re not sure whether a control you have in mind should be a button or a link, describe what it should do to the mentor, and it’ll help you decide:

Read Another Form

These rules let you read a form before you run it. In this plot booking form, Mei has typed 7 in the Plot box and Mei in the Name box:

Language: HTML
<form action="book.html" method="get">
  <p>Plot <input name="plot"></p>
  <p>Name <input></p>
  <button type="button">Help</button>
  <button>Book</button>
</form>

Here’s what the browser does with it:

  • Help has type="button", so clicking it sends nothing. Book has no type, so it’s a submit button.
  • Clicking Book goes to the action with the named fields’ pairs: book.html?plot=7.
  • Mei isn’t in the address, because her box has no name, like your phone box earlier.

Your sign-up form sends its data, but a visitor using a screen reader hears three unnamed text boxes. In the next lesson, Label Every Field, you’ll tie each of those words to its box.

Become a Member to join the conversation.