A JavaScript contact form that emails you every message
A plain form and a short fetch script. The visitor never leaves the page, sees the answer under the button, and every message reaches your inbox, stored in the EU and spam filtered.
14 days free, no card. Paste the address where the code says YOUR_FORM_ID.
<form id="contact" action="https://formhaven.net/f/YOUR_FORM_ID" method="POST">
<input type="email" name="email" required>
<textarea name="message" required></textarea>
<input type="text" name="_gotcha" style="display:none" tabindex="-1" autocomplete="off">
<button type="submit">Send</button>
<p id="status" role="status"></p>
</form>
<script>
const form = document.getElementById('contact');
const status = document.getElementById('status');
form.addEventListener('submit', async (event) => {
event.preventDefault();
const response = await fetch(form.action, {
method: 'POST',
headers: { Accept: 'application/json' },
body: new FormData(form),
});
const { ok, message } = await response.json();
status.textContent = ok ? 'Thanks, your message is on its way.' : message;
if (ok) form.reset();
});
</script>
Set it up
-
1
Create a form in FormHaven
Sign up, press New form and copy its address. It looks like https://formhaven.net/f/YOUR_FORM_ID.
-
2
Paste the form and the script
Put your address in the action. The script reads it from there, so it lives in one place.
-
3
Send a test
Fill in the form. The answer appears under the button and the message lands in your inbox.
-
4
Check it without JavaScript
Turn JavaScript off and send again. The form posts the normal way and the visitor sees a thank-you page, so nobody is left with a dead button.
Show which field is wrong
A refused message comes back with ok set to false and an errors array. Each error has a code and, where it applies, the field. TYPE_EMAIL means the address the visitor typed is not valid, so you can mark that input.
const result = await response.json();
if (!result.ok) {
for (const error of result.errors) {
const input = error.field && form.elements[error.field];
if (input) input.setCustomValidity(error.message);
}
form.reportValidity();
}
Send JSON instead of form data
If your fields live in JavaScript rather than in a form, send a JSON body. The keys become the field names in the email.
const response = await fetch('https://formhaven.net/f/YOUR_FORM_ID', {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify({ email: 'visitor@example.com', message: 'Hello' }),
});
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. new FormData(form) carries the answer it fills in; for a JSON body, copy the _botcheck field's value into it. Visitors see nothing, and it sets no cookies.
<script src="https://formhaven.net/bot-check.js" async></script>
Questions about this setup
- Do I put an API key in the page?
- No. The form's address is meant to be public: it can receive messages, never read them. Spam filtering and the allowed sites setting decide what reaches you.
- Will the browser block the request?
- No. Form addresses accept requests from any website, so fetch works from your own domain, a preview deployment or localhost.
- What does the reply look like?
- Success is ok set to true with a message. A refusal is ok set to false with message, error and an errors array, the same errors array Formspree returns, so code written for Formspree keeps working.
- 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 servers in the EU, for 1 to 365 days as you choose per form, or not at all: then each message is emailed and its answers deleted. 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.