Skip to content

Sapper Guide

Here is guide on integrating a contact form into a Sapper site using Fabform:

Setting Up a Contact Form in Sapper with Fabform

Sapper, built on top of Svelte, offers a dynamic framework for building fast web applications. Integrating a contact form into your Sapper site is straightforward, especially with services like Fabform, which simplify form handling. Let’s walk through the process step by step:

Step 1: Create a New Sapper Site

Start by cloning the Sapper template repository using degit:

Terminal window
npx degit "sveltejs/sapper-template#rollup" my-sapper-site
cd my-sapper-site
npm install

Step 2: Add a Contact Section

Within your Sapper project, create a new file called contact.svelte under the routes directory.

Step 3: Implement the Contact Form

Simple Setup

Replace the contents of contact.svelte with the following code for a basic form setup:

<form accept-charset="UTF-8" action="https://fabform.io/{your-form-endpoint}" method="POST">
<input type="email" name="email" placeholder="Your Email">
<input type="text" name="name" placeholder="Your Name">
<input type="text" name="message" placeholder="Your Message">
<button type="submit">Send</button>
</form>

Using Fetch

For a more dynamic form handling using fetch, utilize the following code block in contact.svelte:

<script>
const YOUR_FABFORM_ENDPOINT = "https://fabform.io/f/{your-form-endpoint}";
let name = "";
let email = "";
let submitting = false;
async function onSubmit() {
try {
submitting = true;
await fetch(YOUR_FABFORM_ENDPOINT, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
name,
email
}),
});
name = "";
email = "";
alert("Form successfully submitted");
} finally {
submitting = false;
}
}
</script>
<form on:submit|preventDefault="{onSubmit}">
<input bind:value="{name}" />
<input bind:value="{email}" />
<button type="submit" disabled="{submitting}">Send</button>
</form>

Step 4: Finalize Setup and Test Locally

Run your Sapper site locally to see the contact form in action:

Terminal window
npm run dev

Visit localhost:3000/contact/ in your browser to test the form. Upon submission, the form data will be sent to Fabform for processing.

Conclusion

You’ve now successfully integrated a contact form into your Sapper site using Fabform. This enhances user interaction and allows visitors to easily reach out to you. Feel free to customize the form further to suit your specific requirements.

Happy coding!


This guide provides a comprehensive walkthrough for integrating a contact form into a Sapper site using Fabform, catering to both basic form setup and dynamic form handling scenarios.