问题
i am now 12 hours on this problem because someone is spamming me with bots. I posted it before but without good results. I checked the code with alot of other examples. The problem starts when someone told me this was wrong ( if($response.success==false)
) and it should be if($result->success==false)
or if($result['success']==false)
but the last two don't work, it doesn't send my form and hangs on 'please wait'. So what is wrong with this, is it something with my Mail code in the if statement, if so, what is it? I can't figure it out.
Thanks for your time. Help is appreciated and please be specific with a answer containing code not just links to another document guide.
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!";
}
}
}
?>
This is the javascript side:
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 );
}
}
I am thinking that inside the javascript code it goes to ELSE and hang on _("status").innerHTML = ajax.responseText;
(Which is 'please wait' what i mentioned above)
回答1:
Without being able to run your code or being able to tell what kind of JSON is being returned, it's definitely not .
. The dot operator is concatenation in PHP and is not overridden to access object properties like it is in many other languages such as JS. json_decode
returns an object whose properties are accessible via the arrow operator ->
. Here's an example:
$data = <<<DATA
{
"fruit": ["banana", "apple", "jackfruit"],
"vegetables": ["broccoli", "asparagus"]
}
DATA;
$food = json_decode($data);
print_r($food->fruit);
print_r($food->vegetables);
As for your conditional, comparing against the boolean false
may not be the way to go--this variable might be a string "false"
.
$response = json_decode('{ "success": "false" }'); // to test your code
if ($response->success === "true") {
// handle CAPTCHA success
}
else {
// handle CAPTCHA failure
}
来源:https://stackoverflow.com/questions/51053286/what-is-wrong-with-my-form-php-recaptcha