1

我让 Netlify 表单工作并接受提交,但是一旦我开始根据https://docs.netlify.com/forms/setup/设置 AJAX ,我无法弄清楚为什么没有收到提交。

我尝试过的事情:

  1. 删除隐藏的“表单名称”输入
  2. 删除验证码
  3. 运行“干净的盖茨比”
  4. 删除打开的 Form 标签的方法属性(方法:“POST”)
  5. 从打开的 Form 标记中删除 action 属性并直接在 handleSubmit 中设置它:.then(() => navigate("/thank-you/"))

非常感谢任何建议或修复!

联系form.js:

import React, { setState } from "react"
import styled from "styled-components"
import Recaptcha from "react-google-recaptcha"
import { navigate } from "gatsby"
import { Button, Col, Form, Row } from "react-bootstrap"
import { breakpoints } from "../utils/breakpoints"

const RECAPTCHA_KEY = process.env.GATSBY_RECAPTCHA_KEY

export default function ContactForm() {
  const [state, setState] = React.useState({})
  const recaptchaRef = React.createRef() // new Ref for reCaptcha
  const [buttonDisabled, setButtonDisabled] = React.useState(true)

  const handleChange = e => {
    setState({ ...state, [e.target.name]: e.target.value })
  }
  const encode = data => {
    return Object.keys(data)
      .map(key => encodeURIComponent(key) + "=" + encodeURIComponent(data[key]))
      .join("&")
  }
  const handleSubmit = e => {
    e.preventDefault()
    const form = e.target
    const recaptchaValue = recaptchaRef.current.getValue()

    fetch("/", {
      method: "POST",
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
      body: encode({
        "form-name": "contact",
        "g-recaptcha-response": recaptchaValue,
        ...state,
      }),
    })
      .then(() => navigate(form.getAttribute("action")))
      .catch(error => alert(error))
  }
  return (
    <Form
      name="contact"
      method="POST"
      netlify
      action="/thank-you"
      netlify-honeypot="bot-field"
      data-netlify-recaptcha="true"
      onSubmit={handleSubmit}
    >
      <Row>
        <Col md={12}>
          <h3>Message Us</h3>
        </Col>
      </Row>
      <Row>
        <Col md={6}>
          <Form.Group hidden>
            <Form.Label htmlFor="bot-field">
              Bot Field: Humans do not fill out!
            </Form.Label>
            <Form.Control name="bot-field" />
            <Form.Control name="form-name" value="contact" />
          </Form.Group>
          <Form.Group>
            <Form.Label htmlFor="first-name">First Name</Form.Label>
            <Form.Control
              required
              size="lg"
              type="text"
              name="first-name"
              onChange={handleChange}
            />
          </Form.Group>
        </Col>
        <Col md={6}>
          <Form.Group>
            <Form.Label htmlFor="last-name">Last Name</Form.Label>
            <Form.Control
              required
              size="lg"
              type="text"
              name="last-name"
              onChange={handleChange}
            />
          </Form.Group>
        </Col>
      </Row>
      <Row>
        <Col md={6}>
          <Form.Group>
            <Form.Label htmlFor="email">Email</Form.Label>
            <Form.Control
              required
              size="lg"
              type="email"
              name="email"
              onChange={handleChange}
            />
          </Form.Group>
        </Col>
        <Col md={6}>
          <Form.Group>
            <Form.Label htmlFor="phone">Phone (Optional)</Form.Label>
            <Form.Control
              size="lg"
              type="tel"
              name="phone"
              onChange={handleChange}
            />
          </Form.Group>
        </Col>
      </Row>
      <Row>
        <Col md={12}>
          <Form.Group>
            <Form.Label htmlFor="message">Message</Form.Label>
            <Form.Control
              required
              as="textarea"
              rows="3"
              placeholder="Enter your message here."
              name="message"
              onChange={handleChange}
            />
          </Form.Group>
        </Col>
      </Row>
      <Row>
        <Col md={12}>
          <FormControls>
            <Recaptcha
              ref={recaptchaRef}
              sitekey={RECAPTCHA_KEY}
              size="normal"
              id="recaptcha-google"
              onChange={() => setButtonDisabled(false)} // disable the disabled button!
              className="mb-3"
            />
            <div>
              <Button className="mr-3" type="reset" value="Eraser">
                Clear
              </Button>
              <Button type="submit" disabled={buttonDisabled}>
                Send
              </Button>
            </div>
          </FormControls>
        </Col>
      </Row>
    </Form>
  )
}
const FormControls = styled.div`
  display: flex;
  align-items: center;
  flex-direction: column;
  @media ${breakpoints.sm} {
    flex-direction: row;
    justify-content: space-between;
  }
  button[disabled] {
    cursor: not-allowed;
  }

盖茨比配置:

require("dotenv").config({
  path: `.env.${process.env.NODE_ENV}`,
})
module.exports = {
  siteMetadata: {
    title: `...`,
    description: `...`,
    author: `...`,
  },
  flags: {
    DEV_SSR: false,
  },
  plugins: [
    `gatsby-plugin-gatsby-cloud`,
    `gatsby-plugin-image`,
    `gatsby-plugin-sharp`,
    `gatsby-plugin-styled-components`,
    `gatsby-plugin-typography`,
    `gatsby-plugin-react-helmet`,
    `gatsby-transformer-sharp`,     
  },
}

静态文件夹中的 HTML 文件:

<form
  data-netlify="true"
  name="contactVivaz"
  method="POST"
  data-netlify-honeypot="bot-field"
  data-netlify-recaptcha="true"
>
  <input type="text" name="first-name" />
  <input type="text" name="last-name" />
  <input type="email" name="email" />
  <input type="tel" name="phone" />
  <textarea name="message"></textarea>
  <div data-netlify-recaptcha="true"></div>
</form>
4

1 回答 1

0

您的状态管理看起来不错,必须拥有(并检查)隐藏字段的输入值,该值必须与 JSX 以及 Netlify 仪表板中的表单名称完全匹配。假设一切都很好命名,看起来,我相信你的问题来自Form组件,它应该看起来像:

<Form
  name="contact"
  method="POST"
  action="/thank-you"
  data-netlify-honeypot="bot-field"
  data-netlify-recaptcha="true"
  data-netlify="true"
  onSubmit={handleSubmit}
>

注意data-netlify-honeypotdata-netlify值。

要添加 reCAPTCHA 字段,您有两种选择:

  • 允许 Netlify 通过简单地添加一个空来处理所有相关逻辑,<div>例如:

        <div data-netlify-recaptcha="true"/>
    
  • 添加自定义 reCAPTCHA(您的情况)在您的 Netlify 仪表板中添加环境变量所需内容(以GATSBY_g-recaptcha-responsePOST

    Netlify 服务器将检查来自该表单的提交,并且仅当它们包含有效的 g-recaptcha-response值时才接受它们。

进一步参考以建立您的设置:

于 2021-05-22T09:10:20.570 回答