Hi there,
I am trying to create a simple contact form with RECAPTCHA that then displays an message when the form has been successfully submitted. To do this, I’ve used a bit of jQuery. The form works fine, but I can’t seem to get the success message to display.
Can anyone see what is wrong?
<script src='https://www.google.com/recaptcha/api.js'></script>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="contact" class="contact-form">
<h1>Contact Us</h1>
<label>Your Name</label>
<input type="text" name="name" required autofocus/>
<label>Email Address</label>
<input type="email" name="email" required/>
<label>Message</label>
<textarea name="message" required></textarea>
<!-- [PUT THE CAPTCHA WHERE YOU WANT IT] -->
<div class="g-recaptcha" data-sitekey="mysitekey"></div>
<input type="submit" value="Submit"/>
<script>
$('.contact-form').submit(function() {
$('#myResultDiv').text("Form submitted");
return false;
});
</script>
<div id="myResultDiv"></div>
</form>
<?php
/* [VERIFY CAPTCHA FIRST] */
$secret = 'mysecretkey'; // CHANGE THIS TO YOUR OWN!
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=".$_POST['g-recaptcha-response'];
$verify = json_decode(file_get_contents($url));
/* [PROCESS YOUR FORM] */
if ($verify->success) {
$to = "me@me.com";
$subject = "Contact Form";
$message = "Name - " . $_POST['name'] . "r\n";
$message .= "Email - " . $_POST['email'] . "r\n";
$message .= "Message - " . $_POST['message'] . "r\n";
if (@mail($to, $subject, $message)) {
// Send mail OK
// @TODO - Show a nice thank you page or something
} else {
// Send mail error
// @TODO - Ask user to retry or give alternative
}
} else {
// Invalid captcha
// @TODO - Show error message, ask user to retry
}
?>
Thank you