简体   繁体   English

我正在尝试使用 React.js 联系表单实现 Netlify 表单

[英]I am trying to implement Netlify form with React.js contact form

I tried multiple methods but was enable to do anything.我尝试了多种方法,但可以做任何事情。 Also referred to this post as well https://www.netlify.com/blog/2017/07/20/how-to-integrate-netlifys-form-handling-in-a-react-app/#troubleshooting-tips也参考这篇文章https://www.netlify.com/blog/2017/07/20/how-to-integrate-netlifys-form-handling-in-a-react-app/#troubleshooting-tips

but nothing helped.但没有任何帮助。 How can I do that?我怎样才能做到这一点?

import React, { Component } from 'react';
import { Grid, Cell, List, ListItem, ListItemContent } from 'react-mdl';
import "bootstrap-css-only/css/bootstrap.min.css";
import Form from 'react-bootstrap/Form';
import Button from 'react-bootstrap/Button';

const encode = (data) => {
    return Object.keys(data)
   .map(key => encodeURIComponent(key) + "=" + encodeURIComponent(data[key]))
   .join("&");
  }


class Contact extends Component {

 constructor(props) {
      super(props);
      this.state = { name: "", email: "", message: "" };
    }

handleSubmit = e => {
      fetch("/", {
        method: "POST",
        headers: { "Content-Type": "application/x-www-form-urlencoded" },
        body: encode({ "form-name": "contact", ...this.state })
      })
        .then(() => alert("Success!"))
        .catch(error => alert(error));

      e.preventDefault();
    };

    handleChange = e => this.setState({ [e.target.name]: e.target.value });

  render() {
    return(
      <div className="contact-body">
        <Grid className="contact-grid">
            <Cell col={12}>
            <h2 style={{textAlign:"center", fontFamily:"Georgia", fontWeight:"bold", fontFamily:"Georgia", fontWeight:"bold"}}>Contact Me</h2>
            <hr className="headinghr"/>
            </Cell>
          <Cell col={4}>
              <div style={{textAlign:'center', paddingTop:'10px'}}>
              <i style={{fontSize: '40px'}} className="fas fa-envelope-square" aria-hidden="true"/>
              <h2 style={{fontSize:'190%', fontFamily: 'Anton', textAlign:'center'}}>E-mail</h2>
              </div>
              <div style={{fontFamily: 'Anton', textAlign:'center', fontSize:'20%'}}>
              <h3 style={{fontSize:'650%'}}>abc@gmail.com</h3>
              </div>
          </Cell>
          <Cell>
          <form onSubmit={this.handleSubmit}>
          <p>
            <label>
              Your Name: <input type="text" name="name" value={name} onChange={this.handleChange} />
            </label>
          </p>
          <p>
            <label>
              Your Email: <input type="email" name="email" value={email} onChange={this.handleChange} />
            </label>
          </p>
          <p>
            <label>
              Message: <textarea name="message" value={message} onChange={this.handleChange} />
            </label>
          </p>
          <p>
            <button type="submit">Send</button>
          </p>
        </form>`
          </Cell>
        </Grid>
      </div>
    )
  }
}
export default Contact;```

I had a similar issue recently, what worked for me was inserting this code snippet into the index.html before id="root"我最近遇到了类似的问题,对我有用的是将此代码段插入到 index.html 之前 id="root"

    <!-- A little help for the Netlify post-processing bots -->
    <form name="contact" netlify netlify-honeypot="bot-field" hidden>
      <input type="text" name="name" />
      <input type="email" name="email" />
      <textarea name="message"></textarea>
    </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 React.js 向联系表格 7 发送帖子请求 - How to send posts requests to contact form 7 from React.js 即使我在 react.js 中添加了验证,我也能够以 antd 步骤形式进入下一步 - I am able to move into next step in antd step form even though i have added validation in react.js 如何在 React.js 中创建登录表单和响应? - How do I create a login form and response in React.js? React.js - 如何在页面加载时提交表单? - React.js - How do I submit a form on page load? 我可以在 Netlify 上托管一个包含 PHP 联系表的网站吗? - Can I host a website on Netlify that includes a PHP contact form? 如何在React.js中提交动态表单 - How to submit a dynamic form in React.js React.js表单值不刷新 - React.js form values not refreshing React.Js 如何重定向表单提交? - React.Js how to redirect on form submit? 我正在尝试使用 Fluent UI 在 React JS 中创建一个表单,但我注意到在日期选择器中我丢失了所有图标 - I am trying to create a form in React JS using Fluent UI but the thing I noticed is that in the date picker I am missing all the icons 反应.js。 当我尝试使用 FileReader 上传超过 3 个图像文件时,仅上传了 3 个 - React.js . when i am trying to upload more then 3 image files, using FileReader, only 3 uploaded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM