function onSubmit(token) { const form = document.querySelector('form[data-form="captcha"]'); const formData = new FormData(form); formData.append("g-recaptcha-response", token); const submitButton = form.querySelector('input[type="submit"]'); const originalButtonText = submitButton.value; submitButton.disabled = true; submitButton.value = "Envoi en cours..."; fetch(form.action, { method: "POST", body: JSON.stringify(Object.fromEntries(formData)), headers: { "Content-Type": "application/json", }, }) .then((response) => response.json()) .then((data) => { handleResponse(data, form); }) .catch((error) => { showFormMessage("error", form); submitButton.disabled = false; submitButton.value = originalButtonText; });}function handleResponse(data, form) { const submitButton = form.querySelector('input[type="submit"]'); const originalButtonText = submitButton.value; if (data.success) { showFormMessage("success", form); form.reset(); } else { showFormMessage("error", form); } submitButton.disabled = false; submitButton.value = originalButtonText;}function showFormMessage(type, form) { form.style.display = "none"; if (type === "success") { const successMessage = form.parentNode.querySelector( '[data-form="success"]' ); if (successMessage) successMessage.style.display = "block"; } else { const errorMessage = form.parentNode.querySelector('[data-form="error"]'); if (errorMessage) errorMessage.style.display = "block"; }}