加载页面时,我在控制台中收到此错误
“ reCAPTCHA找不到用户提供的功能:onloadCallback”
我已经按照phppot的教程使用Google Recaptcha编写了PHP。当我单击提交而不进行重新验证时,它给我一个错误,提示我需要完成它。 但是,当我完成它时,它会以永无休止的循环显示我的加载符号,并且实际上不会发送电子邮件。我在下面有以下代码:
$(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(){}
});
}));
});
在contactus.php页面顶部的部分中。
然后我的contact.php页面具有以下内容:
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 . ">\r\n";
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);
}
}