Written by: Geoffrey Callaghan

How To Add A Contact Form With Eleventy (11ty)

How To Add A Contact Form With Eleventy (11ty)

To add a contact form with Eleventy (11ty) using fabform.io as a backend, you can follow these general steps. Please note that the process may vary based on your specific project structure and requirements.

  1. Create a FabForm.io Account:

    • Go to FabForm.io and sign up for an account.
    • Create a new form that will be used for your contact form.
  2. Get the Form Endpoint:

    • After creating the form, you’ll get a unique form endpoint (URL). This is where the form data will be sent when someone submits the contact form.
  3. Set Up Your Eleventy Project:

    • If you don’t already have an Eleventy project, set it up using the following commands:

      mkdir my-eleventy-project
      cd my-eleventy-project
      npm init -y
      npm install --save-dev @11ty/eleventy
  4. Create Eleventy Project Structure:

    • Create the necessary project structure, including a src folder with an index.njk file for your home page and a contact.njk file for your contact form.

      mkdir src
      touch src/index.njk
      touch src/contact.njk
  5. Create Contact Form HTML:

    • Open the contact.njk file and add your contact form HTML code. Make sure to include the form action as the FabForm.io endpoint.

      ---
      layout: base.njk
      ---
      <h1>Contact Us</h1>
      <form action="YOUR_FABFORM_ENDPOINT" method="POST">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name" required>
      
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required>
      
        <label for="message">Message:</label>
        <textarea id="message" name="message" required></textarea>
      
        <button type="submit">Submit</button>
      </form>
  6. Configure Eleventy:

    • Create a .eleventy.js configuration file in your project root to specify the input and output directories.

      module.exports = function(eleventyConfig) {
        eleventyConfig.addPassthroughCopy("src/assets");
        return {
          dir: {
            input: "src",
            output: "_site",
          },
        };
      };
  7. Run Eleventy:

    • Run Eleventy to generate your site:

      npx eleventy
  8. Preview Your Site:

    • Open the generated _site/index.html and _site/contact.html files in a browser to preview your site.
  9. Deploy Your Site:

    • Deploy your Eleventy site to your preferred hosting provider.

Now, when users submit the contact form, the data will be sent to FabForm.io, and you can manage the submissions through your FabForm.io dashboard.

Remember to replace "YOUR_FABFORM_ENDPOINT" with the actual endpoint provided by FabForm.io. Additionally, you can customize the form and styling based on your project requirements.