JavaScript

Send a form with JavaScript to keep the visitor on the page: the endpoint of the form then answers with JSON rather than redirecting.

Sending a form

Keep the form as for an HTML form: its action is the endpoint, its method is POST, and every field has a name. Then catch its submission, and send the values as FormData with a POST request to the same endpoint. The endpoint answers with JSON when the request doesn't ask for HTML, as fetch does by default.

contact.html

<form id="contact" action="https://submit.backmyform.com/f/k3x9q2mf" method="POST">
	<input type="email" name="email" required />
	<textarea name="message"></textarea>
	<button type="submit">Send</button>
</form>

contact.js

const form = document.querySelector('#contact');

form.addEventListener('submit', async (event) => {
	event.preventDefault();

	const response = await fetch(form.action, {
		method: 'POST',
		body: new FormData(form),
		headers: { Accept: 'application/json' }
	});

	if (response.ok) {
		form.replaceWith('Thank you! We will get back to you soon.');
	} else {
		const error = await response.json();
		alert(error.message);
	}
});

new FormData(form) holds the files of the form too: the request is then sent as multipart/form-data, as needed.

Keep the action on the form

Even when the form is sent by your code, keep its action on the endpoint:

  • the forms.js script finds the BackMyForm forms by their action: without it, the form is ignored, and you get no time to respond, no not finished submission and no partial submission;
  • the script adds a hidden field to the form: build the FormData from the form when it is sent, as above, so that the field goes with it;
  • when your JavaScript fails or is blocked, the browser still sends the form to the endpoint.

The same goes for the frameworks (React, Vue, Svelte, Astro...): render a real <form> with its action, method and the name of its fields, and send new FormData(form) from its submit handler. Show the badge under the form too: it is required on the free plan.

The response

A received submission is answered with a 201 status and its id:

Response

{ "submissionId": "1f0b2c4f-3c4d-6e5f-9a0b-1c2d3e4f5a6b" }

The redirect URL of the form is not used: show your own message.

Errors

A refused submission is answered with an error, in the format of the API. The code tells why:

StatusCodeMeaning
400invalid_resourceThe submission is too large, or a field name or a value is too long. See the limits.
404resource_missingThe form doesn't exist, or is disabled.
413file_too_largeA file is over the size limit; data.maxSize gives it in bytes, when it can be shown.
413too_many_filesThe submission has more files than the form accepts; data.maxFiles gives the limit.
415file_type_not_allowedThe type of a file is not accepted by the form.
415files_not_acceptedThe form doesn't accept files.
415unsupported_media_typeThe body is not FormData nor URL-encoded.
429too_many_requestsToo many submissions from the visitor: wait data.retryAfter seconds.

From a server

The endpoint accepts submissions sent from a server too, for example to forward a form your backend already receives. The submission then has no origin.

curl https://submit.backmyform.com/f/k3x9q2mf \
  -H "Accept: application/json" \
  -d name="John Smith" \
  -d [email protected] \
  -d message="Hello!"

Send a submission from a server

The limits are per IP address: a server sending the submissions of many visitors reaches them sooner than the visitors would.