[英]PHP recaptcha not working
I have set up the google reCaptcha PHP plugin on this site: 我已经在此网站上设置了Google reCaptcha PHP插件:
http://benliger.webatu.com/ http://benliger.webatu.com/
You can see it displays fine under Contact, however after adding in the necessary PHP into my form_process file the form still submits regardless of whether or not the reCaptcha is filled out. 您可以在“联系方式”下看到它显示的很好,但是在将必需的PHP添加到我的form_process文件中之后,无论是否填写了reCaptcha,表单仍将提交。 Here is my PHP code that sits in the form_process file:
这是我的PHP代码,位于form_process文件中:
<?php
//Check if POST data is set, and not empty, else it will do this every single time, submitted or not
require_once('recaptchalib.php');
$privatekey = "6Le2a_oSAAAAAJ81_yQvCelFMIHiUcG_k6u0S1fd";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
if(isset($_POST) && !empty($_POST))
{
$mail_to = 'benliger@hotmail.com'; // specify your email here
// Assigning data from the $_POST array to variables
$name = $_POST['sender_name'];
$mail_from = $_POST['sender_email'];
$phone = $_POST['sender_phone'];
$message = $_POST['sender_message'];
// Construct email subject
$subject = 'enquiry ' . $name;
// Construct email body
$body_message = 'From: ' . $name . "\r\n";
$body_message .= 'E-mail: ' . $mail_from . "\r\n";
$body_message .= 'Phone: ' . $phone . "\r\n";
$body_message .= 'Message: ' . $message;
// Construct email headers
$headers = 'From: ' . $mail_from . "\r\n";
$headers .= 'Reply-To: ' . $mail_from . "\r\n";
$mail_sent = mail($mail_to, $subject, $body_message, $headers);
if ($mail_sent == true){
//Echo the message now, because it will be catched in your jQuery listerener (see code below)
echo 'Thanks for getting in touch!';
} else {
//Echo the message now, because it will be catched in your jQuery listerener (see code below)
echo 'Message not sent :( Please, get in contact with me directly: benliger@hotmail.com';
}
//This exit; is important, else the alert box will be full of the further html code
exit;
}
}
?>
And my HTML: 而我的HTML:
<form name="myForm" action="form_process.php" method="POST">
<?php
require_once('recaptchalib.php');
$publickey = "6Le2a_oSAAAAAEHu4u35QWlLzxzCYB1JnhFoI0u5"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
.... and the rest of my form here ....以及我表格的其余部分
Might be worth noting my send button looks like so: 可能值得注意的是我的发送按钮如下所示:
<input class="sendbutton" type="submit" name="send_message" value="Send">
Javascript: Javascript:
$(document).on('submit','form',function(e){
//Prevent the default action
e.preventDefault();
var form = $(this);
//Create an array of input values
var data = $(this).serializeArray();
//Do the ajax request
$.post('form_process.php',data,function(responseMessage){
resetForm(form);
//Alert your message
$( ".mycontactform" ).html('<p>Thanks for getting in touchaaa!</p>');
//alert(responseMessage);
});
});
Error is in your jquery code .whatever the response is you are replacing it with <p>Thanks for getting in touchaaa!</p>
jQuery代码中有错误。无论响应是什么,您都将其替换为
<p>Thanks for getting in touchaaa!</p>
use alert(responseMessage)
to see the response 使用
alert(responseMessage)
查看响应
so replace $( ".mycontactform" ).html('<p>Thanks for getting in touchaaa!</p>');
因此,替换
$( ".mycontactform" ).html('<p>Thanks for getting in touchaaa!</p>');
with alert(responseMessage)
带
alert(responseMessage)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.