0

所以我正在尝试使用 netlify 表单处理表单请求。一切正常,只是数据没有随表单一起发送。

所以这里是表格

<template>
    <form @submit.prevent="handleSubmit" name="Free Assessment Form" method="post" data-netlify-honeypot="bot-field">
      <span class="slogan">{{ slogan }}</span>
      <input type="hidden" name="form-name" value="contact"  class="hidden"/>
      <input @input="ev => formData.business_name = ev.target.value" type="text" name="Business Name" placeholder="Business Name" :class="{'input-error': error}" @change="error = false">
      <input @input="ev => formData.first_name = ev.target.value" type="text" name="First Name" placeholder="First Name" :class="{'input-error': error}" @change="error = false">
      <input @input="ev => formData.last_name = ev.target.value" type="text" name="Last Name" placeholder="Last Name" :class="{'input-error': error}" @change="error = false">
      <input @input="ev => formData.email = ev.target.value" type="email" name="Email" placeholder="Email" :class="{'input-error': error}" @change="error = false">
      <input @input="ev => formData.phone = ev.target.value" type="text" name="Phone Number" placeholder="Phone Number" :class="{'input-error': error}" @change="error = false">
      <button type="submit" class="form-btn">Submit</button>
    </form>
</template>

<script>
export default {
  name: 'Form',
  props: ['slogan'],
    data() {
    return {
      formData: {},
      error: false
    }
  },
  methods: {
    encode(data) {
    return Object.keys(data)
      .map(key => encodeURIComponent(key) + '=' + encodeURIComponent(data[key]))
      .join('&')
    },
    handleSubmit(e) {
      fetch('/', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: this.encode({
          'form-name': e.target.getAttribute('name'),
              ...this.formData
        }),
      })
      .then(() => {
        this.formData = ""
        this.$router.push('/')
        alert('Form Submitted!')
      })
      .catch(error => alert(error))
    }
  }
}
</script>

现在,一旦提交表单,我就会按预期收到我的电子邮件,但我只得到表单密钥。我没有收到表单键值。

在我的本地开发中,当我将 console.logdata传递到编码方法时,它就在那里,所以我不明白为什么它不起作用。

4

0 回答 0