A contact form for a Cloudflare Pages site
Cloudflare Pages serves your built files, and handling a form there means writing a Pages Function yourself. Point the form at FormHaven instead and every message is spam filtered, stored in Germany and emailed to you, with nothing to deploy beside your HTML.
14 days free, no card. Paste the address where the code says YOUR_FORM_ID.
<form action="https://formhaven.net/f/YOUR_FORM_ID" method="POST">
<label>Your name <input type="text" name="name" required></label>
<label>Your email <input type="email" name="email" required></label>
<label>Message <textarea name="message" required></textarea></label>
<input type="text" name="_gotcha" style="display:none" tabindex="-1" autocomplete="off">
<input type="hidden" name="_next" value="/thanks.html">
<button type="submit">Send message</button>
</form>
Set it up
-
1
Know what Pages does with a form
Cloudflare's Static Forms plugin catches the submission and hands it to a function you write. Storing or emailing it is up to you. Posting to FormHaven skips that code.
-
2
Create a form in FormHaven
Sign up, press New form and copy its address. It looks like https://formhaven.net/f/YOUR_FORM_ID.
-
3
Paste the form and deploy
Put your address in the action, add a thanks.html page, then push to your connected repository or run your usual deploy.
-
4
Send a test
Open your pages.dev address or your own domain, fill in the form and check your inbox.
Allow your preview deployments
Every preview gets its own address, such as 373f31e2.PROJECT.pages.dev, and every branch one like development.PROJECT.pages.dev. If you limit the form to your own sites in its settings, add PROJECT.pages.dev once. Every address that ends in it is allowed too, so previews and branches keep working. Add your own domain beside it.
PROJECT.pages.dev
example.com
Send it with fetch, so the visitor stays on the page
Ask for JSON and FormHaven answers {"ok": true}, or {"ok": false} with a list of errors, each with a message you can show. No Cloudflare code runs for this either.
<script>
document.querySelector('form').addEventListener('submit', async (e) => {
e.preventDefault();
const res = await fetch(e.target.action, {
method: 'POST',
body: new FormData(e.target),
headers: { Accept: 'application/json' },
});
const data = await res.json();
e.target.insertAdjacentText('afterend', data.ok ? 'Thanks, message sent.' : data.errors[0].message);
});
</script>
Stop bots that skip the hidden field
Turn on the bot check in the form's settings and add this line once to the page. Visitors see nothing, and it sets no cookies.
<script src="https://formhaven.net/bot-check.js" async></script>
Questions about this setup
- Do I need a Pages Function or a Worker?
- No. The form posts straight from the visitor's browser to FormHaven. Your project only holds the HTML.
- Does it work on Workers too?
- Yes. Cloudflare now suggests starting new projects on Workers. A static site served by Workers posts the same form the same way, because nothing runs on the host when a visitor presses Send.
- How do I answer a message?
- Press Reply in the email. The address the visitor typed in the email field is set as the reply address.
- Can my client get the messages instead of me?
- Yes. Add up to 5 more addresses to the form. Each one confirms by a link first, and their copy carries no FormHaven branding. You can turn your own copy off.
- Where is the data stored?
- On our server at Hetzner in Falkenstein, Germany, for 1 to 365 days as you choose per form, or not at all. A data processing agreement covers every account.
More to read
Get your form address in a minute
14 days free, no card. Then €1 per form a month, €0.50 from the 11th form.