---
title: "Submit with JavaScript"
canonical: "https://glueforms.com/docs/javascript"
language: "en"
description: "Use fetch, keep fields on errors and retry the same submission safely."
---

# Submit with JavaScript

Use `fetch` to submit without leaving your page. Start with the [HTML form](https://glueforms.com/docs/connect), then add this script after it.

## Confirm receipt

```html
<p id="form-status" role="status" aria-live="polite"></p>
<script type="module">
  const form = document.querySelector('form');
  const status = document.querySelector('#form-status');
  const button = form.querySelector('[type="submit"]');
  let pending = null;
  form.addEventListener('input', () => { pending = null; });
  form.addEventListener('submit', async event => {
    event.preventDefault();
    if (button.disabled) return;
    if (!pending) {
      pending = new FormData(form);
      pending.set('submission_id', crypto.randomUUID());
    }
    const payload = pending;
    button.disabled = true;
    status.textContent = "Sending…";
    try {
      const response = await fetch(form.action, {
        method: 'POST', body: payload, credentials: 'omit',
        headers: { Accept: 'application/json' },
      });
      const receipt = await response.json();
      if (!response.ok || receipt.accepted !== true) throw new Error();
      status.textContent = "Message received.";
      if (pending === payload) { pending = null; form.reset(); }
    } catch {
      status.textContent = "Could not confirm receipt. Your fields are still here. Try again.";
    } finally { button.disabled = false; }
  });
</script>
```

Only show success when the response confirms `accepted: true`. The script keeps fields after an error. A retry keeps the same `submission_id` and payload. Editing a field starts a new submission.

## Handle failures

An exact accepted retry returns its original receipt without another message or quota charge. Reusing an ID with different content returns `409`. `429` can mean monthly quota or request rate protection. Rejected submissions are not saved.

Use [multipart for attachments](https://glueforms.com/docs/attachments). An antispam-protected form also needs its [verification widget](https://glueforms.com/docs/spam).
