AJAX Contact Form (No Page Reload)

A contact form that submits via fetch() instead of a full page reload. Shows inline success or error messages. Great for single-page apps, modals, or anywhere you want a seamless user experience. For full validation and error handling, see the JavaScript contact form guide.

AJAX Contact Form (No Page Reload) preview

Template Code

HTML
<form id="contact-form" action="https://app.formwit.com/api/s/YOUR_FORM_ID" method="POST">
  <label for="name">Name</label>
  <input type="text" id="name" name="name" required />

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

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

  <input type="text" name="_gotcha" style="display:none" tabindex="-1" autocomplete="off" />

  <button type="submit">Send Message</button>
  <p id="form-status" style="display:none"></p>
</form>

<script>
  const form = document.getElementById('contact-form');
  const status = document.getElementById('form-status');

  form.addEventListener('submit', async (e) => {
    e.preventDefault();
    const data = new FormData(form);

    try {
      const response = await fetch(form.action, {
        method: 'POST',
        body: data,
      });

      if (response.ok) {
        status.textContent = 'Message sent successfully!';
        status.style.display = 'block';
        status.style.color = '#16a34a';
        form.reset();
      } else {
        throw new Error('Failed to send');
      }
    } catch (error) {
      status.textContent = 'Something went wrong. Please try again.';
      status.style.display = 'block';
      status.style.color = '#dc2626';
    }
  });
</script>

Want to customize?

Build your own form visually with our free HTML form generator.

Try the Form Generator →

Use cases

  • Single-page applications (SPAs)
  • Modal or popup contact forms
  • Sites where page reloads break the user flow
  • Forms embedded in complex UI layouts

Customization tips

  • Add a loading spinner by disabling the button and showing a spinner element during fetch
  • Customize the success/error messages and colors
  • Add client-side validation before the fetch call for instant feedback
  • Redirect after success with window.location.href = "/thank-you"

Related guide

Want a step-by-step walkthrough? Read the full JavaScript Contact Form Guide.

Related templates

Frequently asked questions

Does this form work without JavaScript?

No. This template requires JavaScript for the fetch-based submission. If JavaScript is disabled, the form won't submit. For a no-JS alternative, use the Basic Contact Form template.

Can I use this with React or Vue?

Yes, but those frameworks have their own form handling patterns. See our React contact form guide for a React-specific approach.

How do I show a loading state?

Disable the submit button and change its text to "Sending..." at the start of the submit handler, then re-enable it in the try/catch finally block.

Get your form working in 30 seconds

  • No credit card required
  • Unlimited forms
  • 100 submissions/month free
Get Started Free