简体   繁体   English

Netlify 表单不适用于 Material-UI 模式

[英]Netlify form doesn't work with a Material-UI modal

I've a simple Next.js app on Netlify that open a form to click on the subscribe button.我在 Netlify 上有一个简单的 Next.js 应用程序,它打开一个表单以单击订阅按钮。

Code代码

Here is the index file:这是索引文件:

pages/index.js

import React from 'react';
import { SubscribeModal } from '../components/SubscribeModal';

export default function Home() {
  const [open, setOpen] = React.useState(false);

  return (
    <div>
      <button onClick={() => setOpen(true)}>Sign in in newsletters</button>
      <SubscribeModal
        open={open}
        onClose={() => setOpen(false)}
        onSuccess={() => setOpenBadNews(true)}
      />
    </div>
  );
}

Here is the modal:这是模态:

components/SubscribeModal.js

import { Dialog, DialogTitle } from '@mui/material';

export function SubscribeModal({ open, onClose, onSuccess }) {
  return (
    <Dialog onClose={onClose} open={open}>
      <DialogTitle>
        <b>Login</b>
      </DialogTitle>
      <form name="contact" action="/success" method="POST" data-netlify="true">
        <input type="hidden" name="form-name" value="contact" />
        <p>
          <label htmlFor="youremail">Your Email: </label>{' '}
          <input type="email" name="email" id="youremail" />
        </p>
        <p>
          <button type="submit">Send</button>
        </p>
      </form>
    </Dialog>
  );
}

I also have a simple pages/success.js app with a success message.我还有一个带有成功消息的简单pages/success.js应用程序。

Problem问题

When I click on the submit button, a 404 page appear.当我点击提交按钮时,会出现一个 404 页面。

Tried solution尝试过的解决方案

  • I've tried every tag in the form tag.我已经尝试了表单标签中的每个标签。
  • I've tried with Next build & Next export and with default Next config deploy.我已经尝试使用 Next build & Next export 和默认的 Next config deploy。
  • I've tried with Material UI component or HTML native elements我试过使用 Material UI 组件或 HTML 本机元素

Someone has an idea?有人有想法吗?

It's because your modal isn't rendered on build.这是因为您的模态未在构建时呈现。

Netlify will analyze your HTML to find the data-netlify="true" tag. Netlify 将分析您的 HTML 以找到data-netlify="true"标签。 But on build, there is no tag because the JavaScript will add the modal when the user click on the button and not at the build time.但是在构建时,没有标签,因为 JavaScript 会在用户单击按钮时而不是在构建时添加模式。

So you can create another page on your Next project like /subscribe with the form.因此,您可以在 Next 项目上创建另一个页面,例如/subscribe表单。 So the form will be rendered at the build time and Netlify will be able to detect your form.因此表单将在构建时呈现,Netlify 将能够检测到您的表单。

I'm not sure if Netlify changed their implementation since the accepted answer but it is now wrong.我不确定自接受答案以来 Netlify 是否更改了它们的实现,但现在是错误的。 I've successfully implemented Netlify forms in my modals by simply following their walkthrough .通过简单地遵循他们的演练,我已经在我的模态中成功地实现了 Netlify 表单。

In any HTML file in your site folder, add an HTML form with the netlify attribute and the input fields you want Netlify to process.在站点文件夹中的任何 HTML 文件中,添加具有 netlify 属性的 HTML 表单和您希望 Netlify 处理的输入字段。

In the JSX form, include an <input type="hidden" name="form-name" value="the-name-of-the-html-form" /> .在 JSX 表单中,包含一个<input type="hidden" name="form-name" value="the-name-of-the-html-form" />

Example:例子:

index.html索引.html

<form name="contact" method="POST" data-netlify="true" netlify-honeypot="bot-field" hidden>
  <input type="hidden" name="form-name" value="contact" />
  <input type="email" name="email" />
  <input type="text" name="name" />
  <textarea name="message"></textarea>
</form> 

SampleModal.js SampleModal.js

<form name="contact" method="POST" data-netlify="true" data-netlify-honeypot="bot-field">
    <input type="hidden" name="form-name" value="contact" />
    <TextField
    id="email"
    name="email"
    margin="dense"
    label="Your Email" />
    <TextField
    id="name"
    name="name"
    margin="dense"
    label="Name" />
    <TextField
    id="message"
    name="message"
    margin="dense"
    label="What do you need?"
    multiline />
    <Button
    variant="contained" 
    color="success"
    type="submit">
        Send Email
    </Button>
</form> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM