pagehaus

Learn HTML · Lesson 7 of 8 · about 15 minutes

A contact form

An email link is fine, but a form is easier for visitors: no email program needed, and they don't have to copy your address. In this lesson you'll build a contact page whose messages arrive in your inbox, without writing any server code.

A form, a label and a box to type in

A form is a <form> element with fields inside. The simplest field is <input>, a one-line text box, and every field needs a <label> that says what to type:

Try it: change the code

What the browser shows

Three attributes tie it together:

id and name are often the same word, as here, but they don't have to be.

The fields of a contact form

A contact form usually asks for three things: a name, an email address to reply to, and the message itself:

Try it: change the code

What the browser shows

Other types of <input> you'll meet: tel for phone numbers, number, date, url for web addresses. Each one gets the right keyboard on a phone.

Choices: menus and checkboxes

When there are only a few possible answers, let people pick instead of typing:

Try it: change the code

What the browser shows

<select> makes a drop-down menu with one <option> per choice. A checkbox is an <input type="checkbox">; its label usually goes after it. Use choices sparingly on a contact form: every extra field is one more reason not to bother.

Make it look like the rest of your site

Browsers draw form fields in their own style, small and grey. A few CSS rules make them easier to use, especially on phones:

Try it: change the code

What the browser shows

Add rules like these to your style.css, with your own colours.

Where does the message go?

So far the form has nowhere to send its answers. That's set with two attributes on <form>:

Something at that address has to receive the message and pass it on to you, and a plain HTML file can't do that by itself. Traditionally it meant writing a program for the server, in PHP or similar. Many hosts and form services now do it for you instead. On Pagehaus, it's built in: point a form at /_form/ followed by any name, such as /_form/contact, and each message is emailed to you and kept on your site's Forms tab. Replying to the email replies to the visitor, thanks to the field called email.

On another host, the idea is the same: the host or form service gives you an address to put in action, and the rest of the form doesn't change.

Build the contact page

Create contact.html in your my-site folder, using your includes from lesson 6:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Contact · Sam Rivera</title>
  <link rel="stylesheet" href="/style.css">
</head>
<body>
  <!--#include virtual="/includes/menu.html" -->

  <h1>Say hello</h1>
  <p>Questions about a recipe, or want a birthday cake? Write to me here.</p>

  <form action="/_form/contact" method="post">
    <p>
      <label for="name">Your name</label><br>
      <input id="name" name="name" required>
    </p>
    <p>
      <label for="email">Your email</label><br>
      <input id="email" name="email" type="email" required>
    </p>
    <p>
      <label for="message">Message</label><br>
      <textarea id="message" name="message" rows="6" required></textarea>
    </p>
    <input type="hidden" name="_subject" value="A message from my website">
    <input type="hidden" name="_redirect" value="/thanks.html">
    <input name="_gotcha" style="display:none" tabindex="-1" autocomplete="off">
    <button>Send</button>
  </form>

  <!--#include virtual="/includes/footer.html" -->
</body>
</html>

The three fields at the end are instructions for Pagehaus, not questions for your visitors. Fields whose names start with an underscore aren't included in the message:

Then add a Contact link to includes/menu.html. Because the menu is included, it appears on every page at once.

A thank-you page

After sending, visitors go to the page in _redirect. Make thanks.html, saying what happens next:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Thank you · Sam Rivera</title>
  <link rel="stylesheet" href="/style.css">
</head>
<body>
  <!--#include virtual="/includes/menu.html" -->

  <h1>Thank you!</h1>
  <p>Your message is on its way. I usually answer within a couple of days.</p>
  <p><a href="/index.html">Back to the home page</a></p>

  <!--#include virtual="/includes/footer.html" -->
</body>
</html>

Without _redirect, Pagehaus shows a plain "Thank you!" page with a link back to your site. Your own looks better, and keeps your menu.

Try it for real

Want a form with different fields? The contact form generator writes one for you to copy, and contact forms in the help has the details.

If it doesn't work

Check your work

One lesson to go: putting your whole site online, at your own address.