All guides

JavaScript fetch

Keep visitors on your page and show the receipt returned by Wiztrom.

Copy your endpoint

Open your form in Wiztrom and copy its endpoint from Setup. Replace the example URL with that address. If you created your form from a template, use the field names and required fields from its Setup HTML.

Give the form a status region

Put the contact form and the status paragraph together in your page. The action still works when JavaScript is unavailable.

HTML
<form id="contact" action="https://forms.wiztrom.com/f/YOUR_FORM_ID" method="post">
  <label>Name <input name="name" required /></label>
  <label>Email <input name="email" type="email" required /></label>
  <label>Message <textarea name="message" required></textarea></label>
  <button type="submit">Send message</button>
</form>
<p id="contact-status" role="status"></p>

Handle the submit event

Place this inside a script tag after the form. FormData includes the named controls and lets the browser set the multipart boundary. The success message includes the submission ID.

JavaScript
const form = document.querySelector('#contact');
const status = document.querySelector('#contact-status');
const button = form.querySelector('button[type="submit"]');

form.addEventListener('submit', async (event) => {
  event.preventDefault();
  if (button.disabled) return;
  button.disabled = true;
  status.textContent = 'Sending…';
  try {
    const response = await fetch(form.action, {
      method: 'POST',
      headers: { Accept: 'application/json' },
      body: new FormData(form),
    });
    const receipt = await response.json();
    if (response.status !== 201 || !receipt.id) throw new Error();
    status.textContent = 'Received. Reference: ' + receipt.id;
    form.reset();
  } catch {
    status.textContent = 'Could not confirm delivery. Your entries are still here.';
  } finally {
    button.disabled = false;
  }
});

Check the failure state

On a test copy, switch the browser to offline and send. The message and email should remain editable. Reconnect, send again and match the displayed reference to the inbox entry.

Find the test in your inbox

On the published page, leave a required field empty and try Send. Fill it in, enter a distinctive test message and submit once. Open the form’s Wiztrom inbox and match the submitted values. If you enabled a notification, open delivery history and check its result.

References