preventDefault()
, then send form data to a custom action URL via JavaScript.To capture and display messages from a custom action URL or API endpoint (such as Stripe) in Webflow, you need to handle form submissions using JavaScript or a backend service. Since Webflow doesn’t support server-side logic directly, you’ll need a workaround. Here’s how:
preventDefault()
event listener.const form = document.querySelector("#donation-form");form.addEventListener("submit", async (e) => { e.preventDefault(); const formData = new FormData(form); const response = await fetch("YOUR_CUSTOM_ACTION_URL", { method: "POST", body: formData, }); const data = await response.json(); handleResponse(data);});
function handleResponse(data) { if (data.status === "success") { window.location.href = "/donation-success"; // Webflow success page } else { document.querySelector("#error-message").textContent = data.message; document.querySelector("#error-message").style.display = "block"; }}
If you want to submit the form to a server-side script and then handle the redirect on the server:
action
attribute to an external processing script (e.g., /process-donation
).?status=success
) and use JavaScript to redirect.Example PHP (server-side example):
if ($chargeSuccessful) { header("Location: /donation-success");} else { header("Location: /donation-failed?message=" . urlencode($errorMessage));}exit;
If redirected with an error message (e.g., /donation-failed?message=Card declined
):
const urlParams = new URLSearchParams(window.location.search);const errorMessage = urlParams.get("message");if (errorMessage) { document.querySelector("#error-message").textContent = errorMessage; document.querySelector("#error-message").style.display = "block";}
To handle Stripe donation responses in Webflow:
This approach ensures a seamless user experience while working within Webflow’s limitations.