Skip to content
Formhook
← Back to blog

Blog

AJAX form submission: send a form without a page reload

· 4 min read

const form = document.querySelector("#contact-form");
form.addEventListener("submit", async (e) => {
  e.preventDefault();
  const res = await fetch(form.action, {
    method: "POST",
    body: new FormData(form),
    headers: { Accept: "application/json" },
  });
  if (res.ok) {
    form.reset();
    document.querySelector("#form-status").textContent = "Thanks - we got it.";
  } else {
    document.querySelector("#form-status").textContent = "Something went wrong. Please try again.";
  }
});

Ship a working form in one line

EU-hosted, submissions kept forever, push notifications on every tier.

Start free

No credit card · read the docs

Keep reading

Cookie preferences

We use a strictly-necessary session cookie and cookieless first-party analytics. With your consent we also run Google Analytics to understand traffic on our marketing pages.