Gatsby Netlify Form

Hello all, I have a Gatsby project hosted on https://auto-search-testing.netlify.app/

I have activated Netlify form and the form is working on the frontend, but when I log in to my dashboard, it is not showing any active form. I have used most resources on the net to debug this, including this: How to Integrate Netlify’s Form Handling in a React App but nothing works pls I need help. Thanks in advance

This is my full code:

import React, { useState } from ‘react’;
import { Link } from ‘gatsby’;
import Layout from ‘…/components/Layout’;
import PageTitle from ‘…/components/PageTitle’;

const ContactPage = () => {
const [name, setName] = useState(“”)
const [email, setEmail] = useState(“”)
const [phone, setPhone] = useState(“”)
const [message, setMessage] = useState(“”)

const handleSubmit = e => {
    e.preventDefault()
    const myForm = e.target;
    const formData = new FormData(myForm);
    fetch("/", {
        method: "POST",
        headers: { "Content-Type": "application/x-www-form-urlencoded" },
        body: new URLSearchParams(formData).toString(),
    })
        .then(() => {
            document.querySelector('.success').innerText =
                "Thank you for reaching out to us, we'd get back to you shortly.";
            // Clear form fields
            setName('');
            setEmail('');
            setPhone('');
            setMessage('');
        })
        .catch((error) => document.querySelector('.error').innerText = 'Something went wrong, pls try again.');
};

return (
    <Layout>
        <PageTitle heading={'Contact Us'} />
        <div className="contact-section">
            <div className="auto-container">
                <div className="row clearfix">
                    {/* Form Column */}
                    <div className="form-column column col-lg-7 col-md-7 col-sm-12 col-xs-12">
                        {/* Contact Form */}
                        <div className="contact-form">
                            <div className="group-title">
                                <h3>DROP US A MESSAGE</h3>
                                <div className="text">
                                    We'd love to hear from you! If you have any questions, feedback, or inquiries, please don't hesitate to get in touch with us. Fill out the form below or use the provided contact information to reach out to our team. We'll do our best to respond to you promptly.
                                </div>
                            </div>
                            {/* Contact Form */}
                            <form name="contact" method="post" data-netlify="true" data-netlify-honeypot="bot-field"
                                onSubmit={handleSubmit} id="contact-form">
                                <input type="hidden" name="form-name" value="contact" />
                                <input type="hidden" name="bot-field" />
                                <div className="row clearfix">
                                    <div className="col-md-6 col-sm-6 col-xs-12 form-group">
                                        <input onChange={({ target }) => setName(target.value)}
                                            type="text"
                                            required
                                            name="name"
                                            value={name}
                                            minLength="3" placeholder="Your Name *" />
                                    </div>

                                    <div className="col-md-6 col-sm-6 col-xs-12 form-group">
                                        <input type="email"
                                            name="email"
                                            value={email}
                                            required
                                            onChange={({ target }) => setEmail(target.value)} placeholder="Your Email *" />
                                    </div>

                                    <div className="col-md-12 col-sm-12 col-xs-12 form-group">
                                        <input type="tel" name="phone" placeholder="Your Phone Number" onChange={({ target }) => setPhone(target.value)}
                                            value={phone} />
                                    </div>

                                    <div className="col-lg-12 col-md-12 col-sm-12 col-xs-12 form-group">
                                        <textarea name="message" placeholder="Message *" required onChange={({ target }) => setMessage(target.value)}
                                            value={message}></textarea>
                                    </div>

                                    <div className="col-lg-12 col-md-12 col-sm-12 col-xs-12 form-group">
                                        <button className="theme-btn btn-style-one" type="submit">
                                            SUBMIT
                                        </button>
                                    </div>
                                </div>
                            </form>
                            <div className="success"></div>
                            <div className="error"></div>
                        </div>
                        {/* End Contact Form */}
                    </div>
                </div>
            </div>
        </div>
    </Layout>
)

}

export default ContactPage;

There’s no form in your HTML source:

view-source:https://auto-search-testing.netlify.app/contact/

Follow this thread:

Hi, thanks for your response, but the thread is just some back and forth still a bit confuse, I have about 4 form on the site so are you suggesting I add the HTML version of each form in the deployment directory? If yes, can you pls provide a guide to do that?

Or is there a way I can configure Gatsby to generate the HTML code for each file in production

Yes, you can generate the HTML code for each file in production by using the gatsby build command. This command builds a version of your site with the production configuration. The built site will be output to the public directory at the root of your project.

Here is the typical build settings for Gatsby on Netlify:

  • Build command: gatsby build
  • Publish directory: public

You can run this command in your local development environment to see the result, or you can configure Netlify to run this command when you deploy your site.

For more information, you can check out the Gatsby on Netlify documentation.

Hey @laravel, I feel your pain here. As you saw, I’m having some trouble with Gatsby forms on Netlify.

One thing I noticed is that you’re using <div>s to wrap your <input> fields. I’ve run into issues with this set up in the past and it’s a simple fix if you use <fieldset> instead to wrap your <input>. I had trouble with accessibility and form recognition without it. Anyway, this probably won’t solve your issue just a tip I found useful. :slight_smile:

Do let me know if you resolve this because I’ve been trying to debug it for over a month now. :persevere:

Thank you guys, I finally resolved the Netlify Forms integration issue in Gatsby. @hrishikesh Your insight regarding Gatsby not rendering the form led me to the root of the problem.

Upon closer debugging, it became evident that Gatsby was rendering only the preloader content, thus preventing the full content from showing up on the page, including the Netlify forms. I decided to remove the preloader, and Gatsby rendered the full page content. Now the forms are correctly displaying on the Netlify dashboard, and everything is functioning seamlessly.

Thank you for the tip. I think there is something getting rendered (maybe a preloader) before the page content. Gatsby only renders the first thing it sees to the ‘‘view page source’’, in my case I display a preloader for 5 secs before the main page content, so Gatsby rendered just the preloader markup, this stopped the main content from getting rendered, including the forms. I hope this helps you. I’m interested in knowing the fix to the problem, so pls keep me posted. Thanks