Written by: Geoffrey Callaghan

How To Create A Contact Form With Gatsby

How To Create A Contact Form With Gatsby

To create a contact form with Gatsby using FabForm.io, you can follow these steps:

  1. Create a FabForm.io Account:

    • Go to FabForm.io and create an account if you don’t have one.
  2. Create a Form on FabForm.io:

    • Once you’re logged in, create a new form by following the instructions on the FabForm.io platform.
  3. Obtain Form Endpoint:

    • After creating the form, you’ll get an endpoint URL provided by FabForm.io. This is where the form data will be submitted.
  4. Create a Gatsby Project:

    • If you don’t have Gatsby installed, install it globally: npm install -g gatsby-cli.
    • Create a new Gatsby project: gatsby new my-gatsby-contact-form.
    • Navigate into the project directory: cd my-gatsby-contact-form.
  5. Install Dependencies:

    • Install necessary dependencies, including axios to make HTTP requests: npm install axios.
  6. Create a Contact Form Component:

    • Create a new file, e.g., src/components/ContactForm.js, and define your contact form component.
    import React, { useState } from 'react';
    import axios from 'axios';
    
    const ContactForm = () => {
      const [formData, setFormData] = useState({
        name: '',
        email: '',
        message: '',
      });
    
      const handleChange = (e) => {
        setFormData({ ...formData, [e.target.name]: e.target.value });
      };
    
      const handleSubmit = async (e) => {
        e.preventDefault();
    
        try {
          // Replace 'YOUR_FABFORM_ENDPOINT' with the actual FabForm.io endpoint
          const response = await axios.post('YOUR_FABFORM_ENDPOINT', formData);
          console.log('Form submitted successfully:', response.data);
        } catch (error) {
          console.error('Error submitting form:', error);
        }
      };
    
      return (
        <form onSubmit={handleSubmit}>
          <label>
            Name:
            <input
              type="text"
              name="name"
              value={formData.name}
              onChange={handleChange}
            />
          </label>
          <br />
          <label>
            Email:
            <input
              type="email"
              name="email"
              value={formData.email}
              onChange={handleChange}
            />
          </label>
          <br />
          <label>
            Message:
            <textarea
              name="message"
              value={formData.message}
              onChange={handleChange}
            />
          </label>
          <br />
          <button type="submit">Submit</button>
        </form>
      );
    };
    
    export default ContactForm;
  7. Integrate the Contact Form Component:

    • Open the src/pages/index.js file or any other page where you want to include the contact form.
    • Import and use the ContactForm component.
    import React from 'react';
    import ContactForm from '../components/ContactForm';
    
    const IndexPage = () => {
      return (
        <div>
          <h1>Welcome to My Gatsby Contact Form</h1>
          <ContactForm />
        </div>
      );
    };
    
    export default IndexPage;
  8. Replace Placeholder Endpoint:

    • Replace 'YOUR_FABFORM_ENDPOINT' in the ContactForm.js file with the actual FabForm.io endpoint obtained in step 3.
  9. Run Your Gatsby Project:

    • Run gatsby develop in the terminal.
    • Open your browser and go to http://localhost:8000 to see your Gatsby site with the contact form.
  10. Test the Contact Form:

    • Fill out the form and submit it to verify that data is being sent to FabForm.io successfully.

Remember to replace placeholders with actual values and adjust the form fields according to your requirements. This is a basic example, and you can customize it further based on your design and functionality preferences.