【问题标题】:Netlify forms not working with my GatsbyJS websiteNetlify 表单不适用于我的 GatsbyJS 网站
【发布时间】:2021-01-15 04:18:18
【问题描述】:

我正在开发一个网站并使用 Netlify 来托管它。我已将 Netlify 表单添加到 contact.js 文件中的联系我们表单中,并且表单名称在 Netlify 表单仪表板中显示良好。虽然,当我提交表单时,什么也没有发生。 Netlify 上没有任何已批准或垃圾邮件中的提交。

这是我的contact.js 源代码。

import React from 'react';
import _ from 'lodash';
import {graphql} from 'gatsby';

import {Layout} from '../components/index';
import {toStyleObj, withPrefix, htmlToReact} from '../utils';
import FormField from '../components/FormField';

// this minimal GraphQL query ensures that when 'gatsby develop' is running,
// any changes to content files are reflected in browser
export const query = graphql`
  query($url: String) {
    sitePage(path: {eq: $url}) {
      id
    }
  }
`;

export default class Page extends React.Component {
    render() {
        return (
            <Layout {...this.props}>
              <article className="post post-full">
                <header className="post-header has-gradient outer">
                  {_.get(this.props, 'pageContext.frontmatter.image', null) && (
                  <div className="bg-img" style={toStyleObj('background-image: url(\'' + withPrefix(_.get(this.props, 'pageContext.frontmatter.image', null)) + '\')')}/>
                  )}
                  <div className="inner-sm">
                    <h1 className="post-title">{_.get(this.props, 'pageContext.frontmatter.title', null)}</h1>
                    {_.get(this.props, 'pageContext.frontmatter.subtitle', null) && (
                    <div className="post-subtitle">
                      {htmlToReact(_.get(this.props, 'pageContext.frontmatter.subtitle', null))}
                    </div>
                    )}
                  </div>
                </header>
                <div className="inner-md outer">
                  <div className="post-content">
                    {htmlToReact(_.get(this.props, 'pageContext.html', null))}
                  </div>
                  <form name={_.get(this.props, 'pageContext.frontmatter.form_id', null)} id={_.get(this.props, 'pageContext.frontmatter.form_id', null)} {...(_.get(this.props, 'pageContext.frontmatter.form_action', null) ? ({action: _.get(this.props, 'pageContext.frontmatter.form_action', null)}) : null)}name="contact" method="POST" data-netlify="true" data-netlify-honeypot="bot-field">
                        <div className="screen-reader-text">
                          <label>Don't fill this out if you're human: <input name="bot-field" /></label>
                        </div>
                        <input type="hidden" name="form-name" value={_.get(this.props, 'pageContext.frontmatter.form_id', null)} />
                        {_.map(_.get(this.props, 'pageContext.frontmatter.form_fields', null), (field, field_idx) => (
                          <FormField key={field_idx} {...this.props} field={field} />
                        ))}
                        <div className="form-submit">
                          <button type="submit" className="button">{_.get(this.props, 'pageContext.frontmatter.submit_label', null)}</button>
                        </div>
                      </form>
                </div>
              </article>
            </Layout>
        );
    }
}

【问题讨论】:

    标签: reactjs gatsby netlify netlify-form


    【解决方案1】:

    您的表单与 Netlify 标准非常不同。在 Netlify 中,要链接您的表单和仪表板,您需要添加一些元数据和元属性,例如 data-netlify="true"data-netlify-honeypot="bot-field"(以避免垃圾邮件)和一个名称与您的 Netlify 表单完全相同的 name 属性你的仪表板。它必须看起来像:

    <form name="contact" method="post" data-netlify="true" data-netlify-honeypot="bot-field">
      {/* You still need to add the hidden input with the form name to your JSX form */}
      <input type="hidden" name="form-name" value="contact" />
      ...
    </form>
    

    查看Form Handling with Static Site GeneratorsHow To Use Netlify Forms With Gatsby (dev.to) 文章。

    【讨论】:

    • 我在代码中有这些属性。它们在极长的
      标签
    • 网络选项卡(在检查器中)中显示您的请求的内容是什么?
    • Netlify 检测到它。每当我按“提交”时,它都会将我带到与联系表单具有相同 URL 的 Netlify 404 屏幕
    • 添加动作属性
    猜你喜欢
    • 1970-01-01
    • 2022-12-23
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 2015-11-06
    相关资源
    最近更新 更多