-
-
Save basamoahjnr/0e0469980c10130ef43229dee7b07be4 to your computer and use it in GitHub Desktop.
Sending a mail via ajax with a captcha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#submit').click(function(e) { | |
// e.preventDefault(); | |
console.log('clicked submit'); | |
var $errors = $('#errors'), | |
$status = $('#status'), | |
name = $('#name').val().replace(/<|>/g, ""), // no xss | |
email = $('#email').val().replace(/<|>/g, ""), | |
msg = $('#message').val().replace(/<|>/g, ""); | |
if (name == '' || email == '' || msg == '') { | |
valid = false; | |
errors = "All fields are required."; | |
} | |
console.log('captcha response: ' + grecaptcha.getResponse()); // pretty sure the problem is in this line | |
if (!errors) { | |
// hide the errors | |
$errors.slideUp(); | |
// ajax to the php file to send the mail | |
$.ajax({ | |
type: "POST", | |
url: "http://orenurbach.com/assets/sendmail.php", | |
data: "email=" + email + "&name=" + name + "&msg=" + msg + "&g-recaptcha-response=" + grecaptcha.getResponse() | |
}).done(function(status) { | |
if (status == "ok") { | |
// slide down the "ok" message to the user | |
$status.text('Thanks! Your message has been sent, and I will contact you soon.'); | |
$status.slideDown(); | |
// clear the form fields | |
$('#name').val(''); | |
$('#email').val(''); | |
$('#message').val(''); | |
} | |
}); | |
} else { | |
$errors.text(errors); | |
$errors.slideDown(); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form class="form" action="javascript:void(0)" novalidate> | |
<!-- all the inputs... --> | |
<!-- captcha --> | |
<div class="input-group"> | |
<div class="g-recaptcha" data-sitekey="6LdOPgYTAAAAAE3ltWQGar80KUavaR-JblgPZjDI"></div> | |
</div> | |
<div class="errors" id="errors" style="display: none"></div> | |
<div class="input-group"> | |
<input type="button" value="Send" class="btn-default right" id="submit"> | |
<div class="clear"></div> | |
</div> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// my email | |
$email_to = '[email protected]'; | |
// getting the data from the post | |
$name = $_POST['name']; | |
$email_from = $_POST['email']; | |
$msg = $_POST['msg']; | |
// subject of the email I'm going to get | |
$subject = 'Message from $name'; | |
// make the message I'm going to see | |
$finalMsg = 'Email from $email_from\n'; | |
$finalMsg .= 'Message:\n\n'; | |
$finalMsg .= '$msg'; | |
$finalMsg .= '\n\nTo respond, send him an email.'; | |
// getting the captcha | |
$captcha; | |
if (isset($_POST['g-recaptcha-response'])) | |
$captcha = $_POST['g-recaptcha-response']; | |
echo 'captcha: '.$captcha; | |
if (!$captcha) | |
echo 'The captcha has not been checked.'; | |
// handling the captcha and checking if it's ok | |
$secret = '6LdOPgYTAAAAAPSWpxQ7xks9yJPYNsNE0XNHSxTc'; | |
$response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true); | |
var_dump($response); | |
// if the captcha is cleared with google, send the mail and echo ok. | |
if ($response['success'] != false) { | |
// send the actual mail | |
@mail($email_to, $subject, $finalMsg); | |
// the echo goes back to the ajax, so the user can know if everything is ok | |
echo 'ok'; | |
} else { | |
echo 'not ok'; | |
} | |
// @mail($email_to, $subject, $finalMsg); | |
// echo 'ok'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment