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.
Create a FabForm.io Account:
Get the Form Endpoint:
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
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
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>
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",
},
};
};
Run Eleventy:
Run Eleventy to generate your site:
npx eleventy
Preview Your Site:
_site/index.html
and _site/contact.html
files in a browser to preview your site.Deploy Your Site:
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.