Lesson 40 of 51 · Module 6, lesson 6 of 6 · 4 min

Where form data goes

Understand action, GET and POST, what really keeps data safe, and how autocomplete helps people.

What you'll learn

  • What action and method do
  • The real difference between GET and POST — and what actually keeps data safe
  • How autocomplete helps people fill in forms faster

Why this matters

Until now, your forms sent their data back to the same page, so that you could see it in the address bar. A real form sends its data to a program on a server, which saves it, emails it, or searches with it. You will write such programs later; here you learn the HTML side.

action: where the data goes

action is the address that receives the data:

html
<form action="https://example.com/newsletter" method="post">

Without action, the form goes to the page it is on — as in all the examples so far. That is why they worked from your computer.

If you set action to a path like /signup and open the page from your computer, the browser looks for a file called signup at the top of your disk and shows "file not found". That is not a bug in your form — there is no server there to receive it.

method: GET or POST

method="get" (the default)method="post"
Where the data goesIn the address: ?q=soupInside the request, not in the address
Can be bookmarked or sharedYesNo
Use it forSearches and filtersSign-ups, messages, payments — anything that changes or saves something
html
<!-- A search: GET, so the results page can be bookmarked -->
<form action="https://example.com/search" method="get">
  <label for="q">Search recipes</label>
  <input id="q" name="q" type="search">
  <button type="submit">Search</button>
</form>

What keeps data safe

A common belief is that POST is "secure". It is not. POST data is not in the address bar, but it travels as openly as GET data. What protects data on its way is HTTPS — the https:// at the start of an address, with the padlock in the browser. Any form that sends personal information must send it to an https:// address.

autocomplete: fill in faster

The autocomplete attribute tells the browser what a field is for, so that it can offer the visitor's saved name, email or address. This helps everyone, and especially people who find typing difficult. Common values:

Fieldautocomplete value
Full namename
Emailemail
Phonetel
Street addressstreet-address
Postcodepostal-code
Countrycountry-name
Password when logging incurrent-password
New password when signing upnew-password
html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Delivery details</title>
</head>
<body>
  <h1>Delivery details</h1>
  <form>
    <p>
      <label for="fullname">Full name</label>
      <input id="fullname" name="fullname" type="text" autocomplete="name" required>
    </p>
    <p>
      <label for="email">Email</label>
      <input id="email" name="email" type="email" autocomplete="email" required>
    </p>
    <p>
      <label for="street">Street address</label>
      <input id="street" name="street" type="text" autocomplete="street-address">
    </p>
    <p><button type="submit">Continue</button></p>
  </form>
</body>
</html>

The "Delivery details" form with three labelled fields — Full name, Email and Street address — and a Continue button.

You may read advice to turn it off with autocomplete="off" on login forms. Do not: browsers ignore it for passwords anyway, and it stops password managers from helping people use strong passwords.

Sending files

A form that uploads a file needs method="post", the attribute enctype="multipart/form-data", and an <input type="file">. You will use it when you work with a server.

Try it yourself

  1. Select the delivery example and choose Try it.
  2. Submit it and read the data. Then add method="post" to the form and submit again: the playground shows POST instead of GET.
  3. In your own browser, open the form and click in the name box. Does the browser offer your saved name?

Common mistakes

  • action="/something" on a page opened from your computer. You get "file not found". Fix: leave out action while learning, or use a full https:// address.
  • Believing POST protects data. Fix: send personal data only to https:// addresses.
  • autocomplete="off" on normal fields. It slows people down. Fix: use the right autocomplete value instead.
  • GET for a form that changes something, such as a sign-up. The data ends up in bookmarks and history. Fix: method="post".

Check yourself

  1. What does a form do if it has no action?
  2. What protects form data while it travels: POST, or HTTPS?
  3. Which autocomplete value fits a new password on a sign-up form?

Answers

  1. It sends the data to the same page.
  2. HTTPS.
  3. new-password.

Next: that completes forms. Build a complete sign-up form in the practice exercise.

Practice project

a course sign-up form

Goal: build an accessible sign-up form for a cooking course, with the right field types and built-in checks.

Your website after this module

Outcome: visitors can sign up through your site, with a form that checks what they type before it sends.

Your my-website folder: the same pages, and signup.html. In module 7 it becomes your contact.html.

Own topic? Make the form sign people up for something on your site, such as a class, a newsletter or a club. Change the three radio buttons to three choices that fit.

Warm up first (optional): Simple Contact Form and Accessible Sign-up Form in Work Space, if it is in your menu. Work Space checks your code at once, so you know you are ready before you build.

What to do

Create signup.html with the full skeleton, an <h1>, and a <form> (no action) that has:

  1. Full name — text, required, autocomplete="name".
  2. Email — type="email", required, autocomplete="email".
  3. Phone — type="tel", autocomplete="tel", and the label says to include the country code.
  4. Course — a group of three radio buttons (for example Soups, Bread, Salads) inside a <fieldset> with a <legend> that asks the question.
  5. Level — a <select> with a first empty option and three levels.
  6. Anything we should know? — a <textarea> with maxlength="500".
  7. A checkbox: "I agree to receive emails about the course".
  8. A submit button with a clear name, such as Sign up.

Every field has a connected <label> (for = id) and a name. Write "(required)" in the labels of required fields.

Open the page from your folder. Test it: submit it empty, then with a wrong email, then correctly — and read the data in the address bar. Paste your code below and submit.

Checklist before you submit

  • Clicking each label moves the cursor into its field.
  • Only one course can be chosen at a time.
  • The browser stops you when a required field is empty.
  • After a correct submit, the address bar shows every field's name and value.
  • You can fill in the whole form with only the keyboard.

Your mentor reviews every submission. The exercise counts as done when it is marked Passed.

Starter code

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sign up for a cooking course</title>
</head>
<body>
  <h1>Sign up for a cooking course</h1>
  <form>
    <p>
      <label for="fullname">Full name (required)</label>
      <input id="fullname" name="fullname" type="text" autocomplete="name" required>
    </p>

    <p>
      <button type="submit">Sign up</button>
    </p>
  </form>
</body>
</html>

Build it in your own folder, on your computer. ZAM students hand this project in on their dashboard, and their mentor reviews it.

Chat with us on WhatsApp