Start with a plain form that posts to the server, then add the nicer behaviour on top.
Scripts fail more often than we like to admit. A slow connection, a blocked file or an old browser can all leave a form half-working. A plain HTML form still sends its data in every one of those cases.
Write the plain version first
<form action="/enquiry" method="post">
<label for="email">Email</label>
<input id="email" name="email" type="email" required />
<label for="message">What do you need?</label>
<textarea id="message" name="message" required></textarea>
<button type="submit">Send</button>
</form>The browser already handles required fields, email checks and the submit button. The server should check everything again, because anyone can send a request without the form.
Then improve it
Once the plain form works, a few lines of script can send it in the background and show a message without reloading the page.
const form = document.querySelector("form");
form.addEventListener("submit", async (event) => {
event.preventDefault();
const res = await fetch(form.action, { method: "POST", body: new FormData(form) });
form.insertAdjacentHTML("afterend", res.ok ? "<p>Thanks, got it.</p>" : "<p>Something went wrong. Please try again.</p>");
});If that script never loads, the form still works. That is the whole point.
