guides How to add a contact form with Eleventy (11ty)

FormBackend is a service that makes it easy to collect submissions from your HTML forms. This makes us an ideal companion when it comes to static site generators.

In this post we’ll take a look at how to add a contact form to a Eleventy static site.

First create your form backend

Log in to your FormBackend account and visit the forms index page. Go ahead and create a new form and give it a name you can remember it by.

After your form has been created, you’ll see the “Submissions” page which is where new submissions will appear. If you navigate to the “Set up” page you can see the unique URL for your form. We’ll copy that!

Create a new Eleventy site

Eleventy is really simple, it takes any file that matches the known extensions and outputs them as .html.

So let’s create a contact.html file in root with the following HTML:

<h1>Contact us</h1>

<form accept-charset="UTF-8" action="https://www.formbackend.com/f/{your-identifier}" method="POST">
  <div class="fieldset">
    <label for="name">Name</label>
    <input type="text" id="name" name="name" required>
  </div>

  <div class="fieldset">
    <label for="email">Email</label>
    <input type="email" id="email" name="email" required>
  </div>

  <div class="fieldset">
    <label for="message">Message</label>
    <textarea id="email" name="email" required></textarea>
  </div>

  <button type="submit">Submit</button>
</form>

Notice the action attribute on the form element. We’ll take the unique URL we copied from FormBackend and paste that in there. This example form has three fields; name, email and message. But your form can have as many or few as you want. All they need is a unique name attribute, which is how we store the data when it hits our servers.

If you run

npx @11ty/eleventy

It’ll install Eleventy and generate the files. In order to start the server you run

npx @11ty/eleventy --serve

You can now visit http://localhost:8080/contact/ which will load the file that was generated from your contact.html file that we just edited.

It’ll look like this

Visiting the Eleventy development server URL in your browser

We can make that look a little better by adding some CSS. Go ahead and add the following <style>-tag after the form at the bottom of the contact.html file.

<style>
  body { font-family: Arial, Helvetica, sans-serif; }

  .fieldset + .fieldset,
  form + form {
    margin-top: 8px;
  }

  label {
    color: #334155;
    display: block;
    font-size: 87.5%;
    font-weight: bold;
    text-transform: uppercase;
  }

  input,
  textarea,
  select {
    border: 1px solid #ddd;
    color: #475569;
    font-size: 100%;
    padding: 5px;
    border-radius: 4px;
  }

  button[ type="submit"] {
    background: purple;
    background: #14b8a6;
    border: none;
    box-shadow: none;
    color: white;
    border-radius: 2px;
    font-size: .8rem;
    text-transform: uppercase;
    font-weight: 500;
    padding: 8px 12px;
    margin-top: 16px;
  }
</style>

You should now see this if you reload the page:

The styled contact form

After filling out the form and hitting the submit button, you’ll be taken to FormBackend’s submission success page and if you navigate to the Submissions page for the form you just created earlier in FormBackend you should see the data you just submitted.

We can do more!

There are so many more things you can do with FormBackend and we’ve just explored the surface.

If you want to send an auto-response email to the submitter when they submit your form, navigate to the Notifications tab and go to the “Send email to the submitter” section.

You can also send an email to yourself with the submission. We even allow for attachments and will attach those to the email.

Or you can use webhooks, integrate with Zapier or send the submissions to Google Sheets or Slack using our native integrations.

Every submission is of course checked to see if it’s spam and if it is it’s filtered into the spam inbox.