Netlify Form + NextJS + Formik: Form is detected, but submissions aren't coming through

Site name: ashtonguidebeck-app

I have a fairly simple Nextjs site (using the Pages directory). I have a contact form built with Formik. The form is detected, as is the honeypot field. But for the life of me, I can’t get a single submission to come through. I am getting back a 200 response, but no submission is showing up in the dashboard. I’ve spent 3 days trying to troubleshoot this issue. I’ve read the docs, I’ve read MANY Support Forums here, and nothing helps/works.

Things I have and have done so far:

  • Form has data-netlify="true" and data-netlify-honeypot="bot-field" attributes.
  • Form has a hidden field with the form name.
  • Form has a visually hidden “bot-field”
  • A static html file added to the public directory within the project, with a hidden copy of the form, for form detection (which is working).
  • Included the form name and bot-field value in the AJAX response on submit.
  • Submitted test responses with correct/non-spam copy to ensure they go to valid submissions.
  • Checked spam submissions (nothing there).

The live version of the site was a single page, built with create react app, and the form submission was working. This new work with a next js/multipage set up isn’t live yet, but I’ve been testing through deploy previews.

Here’s what I have in my code.

ConnectForm.tsx

<Formik
  initialValues={initialValues}
  validate={(values: FormValues) => {
    const errors = {} as FormErrors;
    if (!values.fullName) {
      errors.fullName = 'Required';
    }
    if (!values.email) {
      errors.email = 'Required';
    } else if (
      !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(values.email)
    ) {
      errors.email = 'Invalid email address';
    }
    if (!values.message) {
      errors.message = 'Required';
    }
    return errors;
  }}
  onSubmit={(values: FormValues, { setSubmitting, resetForm }) => {
    setSubmitting(true);

    const botField = document.getElementsByName(
      'bot-field'
    )[0] as HTMLInputElement;

    if (botField && botField.value !== '') {
      values['bot-field'] = botField.value;
    }

    netlifySubmit(values)
      .then(() => {
        setTimeout(() => {
          resetForm();
          setMessage(
            'Thank you for your message. I will be in touch shortly.'
          );
          window.scrollTo({
            top: 100,
            left: 100,
            behavior: 'smooth',
          });
          setSubmitting(false);
        }, 500);
      })
      .catch((error) => {
        // eslint-disable-next-line no-console
        console.log(error);
        setTimeout(() => {
          setMessage(
            `There was an error with your submission${
              error ? `: ${error}` : '.'
            }`
          );
          setSubmitting(false);
        }, 500);
      });
  }}
>
  {({
    values,
    errors,
    touched,
    handleChange,
    handleBlur,
    handleSubmit,
    isSubmitting,
    /* and other goodies */
  }) => (
    <form
      name="connect"
      onSubmit={handleSubmit}
      data-netlify="true"
      data-netlify-honeypot="bot-field"
    >
      <input
        type="hidden"
        name="form-name"
        value={values['form-name']}
      />
      <VisuallyHidden as="div">
        <label htmlFor="bot-field">
          Don&apos;t fill this out if you&apos;re human:
          <input name="bot-field" />
        </label>
      </VisuallyHidden>
      <InputWrap
        id="fullName"
        label="Full Name"
        error={errors.fullName}
        touched={touched.fullName}
      >
        <input
          id="fullName"
          type="text"
          name="fullName"
          placeholder="FULL NAME*"
          required
          onChange={handleChange}
          onBlur={handleBlur}
          value={values.fullName}
        />
      </InputWrap>
      <InputWrap
        id="email"
        label="Email"
        error={errors.email}
        touched={touched.email}
      >
        <input
          id="email"
          type="email"
          name="email"
          placeholder="EMAIL ADDRESS*"
          required
          onChange={handleChange}
          onBlur={handleBlur}
          value={values.email}
        />
      </InputWrap>
      <InputWrap
        type="textarea"
        id="message"
        label="Message"
        error={errors.message}
        touched={touched.message}
      >
        <textarea
          id="message"
          name="message"
          placeholder="TYPE YOUR MESSAGE HERE."
          required
          onChange={handleChange}
          onBlur={handleBlur}
          value={values.message}
        />
      </InputWrap>
      <Styled.Submit type="submit" disabled={isSubmitting}>
        {isSubmitting ? 'Submitting' : 'Submit'}
      </Styled.Submit>
    </form>
  )}
</Formik>

netlifySubmit.ts

/**
 * send form values to netlify
 * @param {object} values
 */
const netlifySubmit = async (values: any) => {
  const encode = (data: any) => {
    return Object.keys(data)
      .map(
        (key) => encodeURIComponent(key) + '=' + encodeURIComponent(data[key])
      )
      .join('&');
  };

  return fetch('/connect', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: encode(values),
  });
};

export default netlifySubmit;

public/static-form.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#6C674B" />
    <meta name="robots" content="noindex" />
    <meta
      name="description"
      content="'Systemically Trained Therapist | Transitioning out of Clinical Roles'"
    />
    <link rel="apple-touch-icon" href="/favicons/apple-touch-icon.png" />
    <link rel="manifest" href="/site.webmanifest" />
    <title>Ashton Guidebeck</title>
  </head>
  <body>
    <!-- Hidden Form for Netlify -->
    <form name="connect" netlify netlify-honeypot="bot-field" hidden>
      <label htmlFor="bot-field">
        Don&apos;t fill this out if you&apos;re human:
        <input name="bot-field" />
      </label>
      <input type="text" id="fullName" name="fullName" />
      <input type="email" id="email" name="email" />
      <textarea id="message" name="message"></textarea>
    </form>

    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>

Again, just looking to see if I’m missing anything as I’ve been staring at this code for days and still can’t get it working.

We need to know the site name to check anything further.

Oops! Forgot to include it. I’ve updated the description. Site name is ashtonguidebeck-app.

This is a problem with your code. Your code never moves beyond this point:

It never attmpts to submit the form, and there’s no network request being made. This isn’t a Netlify issue and we cannot provide support debugging your custom code.

As I mentioned in the thread description, the code I’m having trouble with isn’t live. The error you’ve captured is from my create react app, single page site code that is currently live. That site is connected to a form called “Contact”.

I’ve rebuilt the site using next js, to incorporate a CMS and multiple pages, and this is where the issue is I’m trying to solve. If it needs to be live in order to receive support through this thread, I can push it live. Otherwise, I’ve been testing against a deploy preview. (Connect | Ashton Guidebeck). You’ll see the network request comes through, and no console error on form submission. This new code is pointing to a form names “Connect”.

Well, when I asked you to share the link, I assumed you’d share the link that you’re currently working on. My apoligies, I should have checked further.

But the problem with your deploy preview is documented:

You’re using SSR for the home page:

image

Thus, the issue.

Thank you! Pointing the fetch endpoint to the static html file for the form worked.

I had a feeling it had something to do with my endpoint, but wasn’t sure what! I actually tried pointing to the static file at one point in my troubleshooting, but never pushed it up to test on my deploy preview as I was getting a 405/“Not Allowed” error locally. But of course, now that I pushed it up, it worked.

Thank you for taking the time to help me! It’s much appreciated.