This php code emails the form contents to the intended recipient. I would think that I could shoehorn in some form post-processing (js page redirects, perhaps) in the script below. Like after "DO SOMETHING AFTER FORM SUBMISSION". I tried this and it worked!
<!-- (B) AJAX SUBMISSION -->
<script>
function doajax () {
// (B1) GET FORM DATA
var data = new FormData(document.getElementById("cform"));
// REQUIRED: APPEND CAPTCHA RESPONSE
data.append("g-recaptcha-response", grecaptcha.getResponse());
// (B2) AJAX FETCH
fetch("process.php", { method: "POST", body: data })
.then((res) => { alert ('message sent'); window.location.href = "index.php"; })
});
return false;
}
</script>
HTML:
<form id="cform" method="post" onsubmit="return doajax();">
<input type="text" name="name" required/>
<input type="email" name="email" required/>
<textarea name="message" required></textarea>
<div class="g-recaptcha" data-sitekey="--- SITE KEY ---></div>
<input type="submit" id = "submit" value="Submit"/>
</form>
process.php
<?php
// (B) VERIFY CAPTCHA
$secret = "---google recaptcha secret ---";
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=".$_POST["g-recaptcha-response"];
$verify = json_decode(file_get_contents($url));
if (!$verify->success) { $error = "Invalid captcha"; }
// (C) SEND MAIL
$mailTo = "me@gmail.com";
$mailSubject = "Contact Form";
$mailBody = "";
foreach ($_POST as $k=>$v) {
if ($k!="g-recaptcha-response") { $mailBody .= "$k: $v\r\n"; }
}
if (!@mail($mailTo, $mailSubject, $mailBody)) { $error = "Failed to send mail";}
?>