我现在在这个问题上已经12个小时了,因为有人用机器人向我发送了垃圾邮件。我以前发布过,但是没有很好的结果。我检查了很多其他示例的代码。当有人告诉我这是错误的(if($response.success==false)
时,问题就开始了,应该是if($result->success==false)
或if($result['success']==false)
,但是后两个不起作用,它不会发送我的表格并挂起在“请稍候”。那么,这有什么问题呢,如果if语句中的我的邮件代码有问题,如果是,那是什么?我不知道。
感谢您的宝贵时间。感谢您的帮助,请提供包含代码的答案,而不仅仅是链接到另一个文档指南。
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=(SECRETKEY)&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
if($response.success==false)
{
echo '<span id="status" style="font-size:1vmax;color:red;">ReCaptcha ERROR</span>';
}else
{
if( isset($_POST['n']) && isset($_POST['e']) && isset($_POST['mn']) && isset($_POST['m']) ){
$n = $_POST['n']; // HINT: use preg_replace() to filter the data
$e = $_POST['e'];
$mn = $_POST['mn'];
$m = nl2br($_POST['m']);
$to = "gesternl@gester.nl";
$from = $e;
$subject = 'Contact Formulier-eng';
$message = '<b>Naam:</b> '.$n.' <br><b>Email:</b> '.$e.' <br><b>Mobiel-nummer:</b> '.$mn.' <p>'.$m.'</p>';
$headers = "Van: $from\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if( mail($to, $subject, $message, $headers) ){
echo "success";
} else {
echo "The server failed to send a message. Please try again later. Thank you!";
}
}
}
?>
这是JavaScript方面:
function submitForm(){
var recaptcha = $("#g-recaptcha-response").val();
if (recaptcha === "") {
event.preventDefault();
//alert("Check ReCaptcha");
document.getElementsByClassName("status-eng-error")[0].innerHTML = "<span style='font-size:15px;color:red;'>" + "Please check ReCaptcha" + "</span>";
}
else{
"use strict";
_("mybtn").disabled = true;
_("status-eng").innerHTML = 'One moment please ...';
var formdata = new FormData();
formdata.append( "n", _("n").value );
formdata.append( "e", _("e").value );
formdata.append( "mn", _("mn").value );
formdata.append( "m", _("m").value );
var ajax = new XMLHttpRequest();
ajax.open( "POST", "contactform-eng.php" );
ajax.onreadystatechange = function() {
if(ajax.readyState == 4 && ajax.status == 200) {
if(ajax.responseText == "success"){
_("my_form").innerHTML = '<h2>Thankyou '+_("n").value+'. Your message has been sent successfully. We will respond to your message as soon as possible.<pre>Have a nice day!</pre></h2>';
} else {
_("status").innerHTML = ajax.responseText;
_("mybtn").disabled = false;
}
}
};
ajax.send( formdata );
}
}
我正在考虑在javascript代码中转到ELSE并挂在_("status").innerHTML = ajax.responseText;
上(这是“请稍候”我上面提到的内容)
答案 0 :(得分:1)
如果无法运行您的代码或不知道返回的是哪种JSON,则肯定不是.
。点操作符在PHP中是串联的,不会像在其他许多语言(如JS)中那样被覆盖以访问对象属性。 json_decode
返回一个对象,该对象的属性可通过箭头运算符->
访问。这是一个示例:
$data = <<<DATA
{
"fruit": ["banana", "apple", "jackfruit"],
"vegetables": ["broccoli", "asparagus"]
}
DATA;
$food = json_decode($data);
print_r($food->fruit);
print_r($food->vegetables);
对于您的条件而言,与布尔值false
进行比较可能不是可行的方法-该变量可能是字符串"false"
。
$response = json_decode('{ "success": "false" }'); // to test your code
if ($response->success === "true") {
// handle CAPTCHA success
}
else {
// handle CAPTCHA failure
}