我在此网站上设置了Google reCaptcha PHP插件:
你可以看到它在Contact下显示正常,但是在将必要的PHP添加到我的form_process文件后,无论是否填写了reCaptcha,表单仍然会提交。这是我的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;
}
}
?>
我的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);
?>
....以及我在此表格的其余部分
可能值得注意我的发送按钮如下:
<input class="sendbutton" type="submit" name="send_message" value="Send">
使用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);
});
});
答案 0 :(得分:2)
错误发生在您的jquery代码中。无论您的响应是什么,都要替换它
<p>Thanks for getting in touchaaa!</p>
使用alert(responseMessage)
查看回复
所以请替换$( ".mycontactform" ).html('<p>Thanks for getting in touchaaa!</p>');
alert(responseMessage)