When I load my page, I get this error in the console
"reCAPTCHA couldn't find user-provided function: onloadCallback"
I have followed a tutorial from phppot for a PHP with Google Recaptcha. When I click submit without doing the recaptcha, it gives me an error saying that I need to complete it. However, when I do complete it, it shows my loading symbol in a never ending loop and doesn't actually send the email. I have the following codes below:
$(document).ready(function (e){
$("#frmContact").on('submit',(function(e){
e.preventDefault();
$("#mail-status").hide();
$('#send-message').hide();
$('#loader-icon').show();
$.ajax({
url: "contact.php",
type: "POST",
dataType:'json',
data: {
"name":$('input[name="name"]').val(),
"email":$('input[name="email"]').val(),
"phone":$('input[name="phone"]').val(),
"content":$('textarea[name="content"]').val(),
"g-recaptcha-response":$('textarea[id="g-recaptcha-response"]').val()},
success: function(response){
$("#mail-status").show();
$('#loader-icon').hide();
if(response.type == "error") {
$('#send-message').show();
$("#mail-status").attr("class","error");
} else if(response.type == "message"){
$('#send-message').hide();
$("#mail-status").attr("class","success");
}
$("#mail-status").html(response.text);
},
error: function(){}
});
}));
});
is in my section on top of my contactus.php page.
Then I have the following for my contact.php page:
if($_POST)
{
$user_name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$user_phone = filter_var($_POST["phone"], FILTER_SANITIZE_STRING);
$content = filter_var($_POST["content"], FILTER_SANITIZE_STRING);
if(empty($user_name)) {
$empty[] = "<b>Name</b>";
}
if(empty($user_email)) {
$empty[] = "<b>Email</b>";
}
if(empty($user_phone)) {
$empty[] = "<b>Phone Number</b>";
}
if(empty($content)) {
$empty[] = "<b>Comments</b>";
}
if(!empty($empty)) {
$output = json_encode(array('type'=>'error', 'text' => implode(", ",$empty) . ' Required!'));
die($output);
}
if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
$output = json_encode(array('type'=>'error', 'text' => '<b>'.$user_email.'</b> is an invalid Email, please correct it.'));
die($output);
}
//reCAPTCHA validation
if (isset($_POST['g-recaptcha-response'])) {
require('component/recaptcha/src/autoload.php');
$recaptcha = new \ReCaptcha\ReCaptcha(SECRET_KEY, new \ReCaptcha\RequestMethod\SocketPost());
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$resp->isSuccess()) {
$output = json_encode(array('type'=>'error', 'text' => '<b>Captcha</b> Validation Required!'));
die($output);
}
}
$toEmail = "member@testdomain.com";
$mailHeaders = "From: " . $user_name . "<" . $user_email . ">
";
if (mail($toEmail, "Contact Mail", $content, $mailHeaders)) {
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .', thank you for the comments. We will get back to you shortly.'));
die($output);
} else {
$output = json_encode(array('type'=>'error', 'text' => 'Unable to send email, please contact'.SENDER_EMAIL));
die($output);
}
}