Written by: Geoffrey Callaghan

How to add a contact form to your Hugo website

How to add a contact form to your Hugo website

To create a contact form in a Hugo website and use FabForm.io as the form handling service, you can follow these general steps:

  1. Create a Form on FabForm.io:

    • Go to FabForm.io, sign up or log in.
    • Create a new form and configure the form fields as needed.
    • Get the form endpoint URL provided by FabForm.io. This URL will be used to submit form data.
  2. Add Form HTML in Hugo:

    • In your Hugo project, create a new HTML file where you want to include the contact form (e.g., contact.html).
    • Add the HTML code for your contact form. Ensure that the form action is set to the FabForm.io endpoint.
    <form action="YOUR_FABFORM_IO_ENDPOINT" method="POST">
        <!-- Your form fields go here -->
        <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>
    
        <!-- Add more fields as needed -->
    
        <button type="submit">Submit</button>
    </form>
  3. Submit Form Data with JavaScript:

    • To enhance the user experience, you can use JavaScript to asynchronously submit the form data to FabForm.io without refreshing the page.
    <script>
        const form = document.querySelector('form');
        form.addEventListener('submit', async function (e) {
            e.preventDefault();
            
            const formData = new FormData(form);
            const response = await fetch('YOUR_FABFORM_IO_ENDPOINT', {
                method: 'POST',
                body: formData,
            });
    
            // Handle the response as needed
            console.log(response);
        });
    </script>
  4. Deploy and Test:

    • Deploy your Hugo website to see the contact form in action.
    • Test the form to ensure that submissions are being sent to FabForm.io.

Remember to replace 'YOUR_FABFORM_IO_ENDPOINT' with the actual endpoint URL provided by FabForm.io. Additionally, customize the form fields based on your requirements. Always consider adding client-side and server-side validation for security.

Provide your email address on the form

If you would like to give the option for people to contact you by email too. You can use VeilMail.io to hide email address with captcha. VeilMail makes sure it’s a human reading your email address and not a bot or a email scraper.