跳到正文
DEV Community· Harsimran jeet Singh·· 10 小時前AI 評分53

如何在 Netlify、Vercel 與 Cloudflare Pages 上為靜態網站加入 Formkove 聯絡表單

Static site contact form: Netlify, Vercel, Cloudflare Pages

AI 導讀

這篇教學示範如何使用 Formkove,為 Netlify、Vercel 或 Cloudflare Pages 上的靜態網站加入聯絡表單。內容涵蓋設定允許來源與重新導向、加入 HTML 表單,或用 JavaScript 留在原頁顯示提交結果。Formkove 免費方案每月有 300 次提交額度,並提供 AI 垃圾郵件過濾及 honeypot,也可加設 Cloudflare Turnstile。

正文

A static site has no server of its own to receive a form. On Netlify, Vercel and Cloudflare Pages you have three ways to handle one:

Option Trade-off
Your host's form feature Netlify has one (Netlify Forms). Vercel and Cloudflare Pages don't.
A serverless function Netlify Functions, Vercel Functions or Pages Functions. Full control, but you build email delivery, spam filtering and storage yourself.
A form backend Keep your own HTML. The backend receives the POST, filters spam and emails you. It works the same on any host.

This guide uses a form backend, Formkove, which has a free plan with 300 submissions a month. The HTML is the same on all three hosts; the differences are in step 2. On GitHub Pages or Jekyll? Follow How to add a contact form to GitHub Pages instead.

1. Create a form

Sign up free, click New form, and copy the endpoint URL:

https://app.formkove.com/api/forms/YOUR_FORM_ID/submissions

Submissions go to your account email by default. To add another address, use Settings → Submission notifications and confirm it from the verification email.

2. Allow your site's address

In the form's Settings → Allowed Origins & Redirects, add the address your site runs on. You need this to redirect to your own thank-you page or to submit with JavaScript.

  • Netlify: https://SITE.netlify.app, plus your custom domain.
  • Vercel: https://PROJECT.vercel.app, plus your custom domain.
  • Cloudflare Pages: enter the bare domain PROJECT.pages.dev. A bare domain also matches its subdomains, so preview deployments like abc123.PROJECT.pages.dev work too.

Netlify and Vercel preview URLs use their own hostnames. If you test on a preview, add that address as well.

3. Add the HTML

Paste this into your contact page and replace YOUR_FORM_ID and the redirect address:

<form action="https://app.formkove.com/api/forms/YOUR_FORM_ID/submissions?redirect=https://example.com/thanks/" method="POST">
  <label for="name">Name</label>
  <input id="name" name="name" type="text" autocomplete="name" required>

  <label for="email">Email</label>
  <input id="email" name="email" type="email" autocomplete="email" required>

  <label for="message">Message</label>
  <textarea id="message" name="message" rows="5" required></textarea>

  <!-- Honeypot: hidden from people, filled in by bots. Leave it empty. -->
  <div aria-hidden="true" style="position:absolute; left:-9999px">
    <label>Leave this empty <input type="text" name="_gotcha" tabindex="-1" autocomplete="off"></label>
  </div>

  <button type="submit">Send</button>
</form>

Then create the /thanks/ page. The redirect must be a full URL on an address you allowed in step 2. Without ?redirect=, visitors see Formkove's success page.

Each field's name becomes a column in your dashboard. The email field becomes the reply-to address on the notification, so you can answer by replying to the email.

On Netlify: don't add netlify or data-netlify="true" to this form. Those attributes hand the form to Netlify Forms instead.

With a framework: the same form works in any framework that outputs HTML. There are guides for Next.js, Astro, React, Vue, Svelte and Hugo, Jekyll and Eleventy. The form ID is safe to put in public HTML: it can only send submissions to its own form.

4. Or show a message without leaving the page

To keep visitors on the page, remove ?redirect=… from the action and add:

<script>
  const form = document.querySelector("form");
  form.addEventListener("submit", async (event) => {
    event.preventDefault();
    const res = await fetch(form.action, {
      method: "POST",
      headers: { Accept: "application/json" },
      body: new FormData(form),
    });
    form.innerHTML = res.ok
      ? "<p>Thanks! We'll be in touch.</p>"
      : "<p>Something went wrong. Please try again.</p>";
  });
</script>

This needs your site's address under Allowed Origins (step 2), or the browser blocks the response.

5. Keep spam out

Every Formkove form is rate limited and checked by an AI spam filter, which holds spam in your dashboard instead of emailing it to you. New forms also have the _gotcha honeypot turned on, which the hidden input in step 3 uses. If bots still get through, add Cloudflare Turnstile with your own keys.

Submissions the AI flags as spam count toward your monthly limit; ones stopped by the honeypot or Turnstile don't. How to stop contact form spam explains what each layer catches.

Troubleshooting

  • You land on Formkove's page instead of yours: the redirect address isn't allowed, or isn't a full https:// URL.
  • A CORS error in the console with fetch(): the address you're testing on isn't under Allowed Origins. Previews count as separate addresses.
  • On Netlify, submissions show up in Netlify instead: remove the netlify attribute from the form and redeploy.
  • No email arrives: check your spam folder for the first one, and confirm the recipient address if you added a new one. Submissions always appear in your dashboard.

More fixes are in troubleshooting.


Originally published on formkove.com.

來源:DEV Community · dev.to