[英]How to implement reCAPTCHA with AJAX for sending mail in php?
我正在嘗試在我網站的聯系表單中添加 Google 的驗證碼。 我使用<input type="submit">
而不是按鈕標簽來提交表單。 但它不適用於 AJAX。 使用按鈕標簽時,驗證碼響應始終為空。 您可以在vipiny.com上檢查問題。
這是聯系表格。
<form action="" method="post" id="contactForm" name="contactForm">
<div>
<label for="contactName">Name <span class="required">*</span></label>
<input type="text" value="" size="35" id="contactName" name="contactName">
</div>
<div>
<label for="contactEmail">Email <span class="required">*</span></label>
<input type="text" value="" size="35" id="contactEmail" name="contactEmail">
</div>
<div>
<label for="contactSubject">Subject</label>
<input type="text" value="" size="35" id="contactSubject" name="contactSubject">
</div>
<div>
<label for="contactMessage">Message <span class="required">*</span></label>
<textarea cols="50" rows="3" id="contactMessage" name="contactMessage"></textarea>
</div>
<div class="g-recaptcha captcha" data-theme="light" data-sitekey="6LfAkhQUAAAAAC2D3LxhB9XtYeoJGhuvR31sq9HW"></div>
<div>
<button class="submit">Submit</button>
</div>
</form> <!-- Form End -->
我正在使用 ajax 將數據發送到 sendMail.php 文件。
$('form#contactForm button.submit').click(function() {
$('#image-loader').fadeIn();
var contactName = $('#contactForm #contactName').val();
var contactEmail = $('#contactForm #contactEmail').val();
var contactSubject = $('#contactForm #contactSubject').val();
var contactMessage = $('#contactForm #contactMessage').val();
var data = 'contactName=' + contactName + '&contactEmail=' + contactEmail +
'&contactSubject=' + contactSubject + '&contactMessage=' + contactMessage;
$.ajax({
type: "POST",
url: "inc/sendEmail.php",
data: data,
success: function(msg) {
// Message was sent
if (msg == 'OK') {
$('#image-loader').fadeOut();
$('#message-warning').hide();
$('#contactForm').fadeOut();
$('#message-success').fadeIn();
}
// There was an error
else {
$('#image-loader').fadeOut();
$('#message-warning').html(msg);
$('#message-warning').fadeIn();
}
}
});
return false; }); });
到目前為止,這是我的 sendMail.php 文件。 我試圖驗證如果$error['captcha']
為空以及其他輸入字段檢查,郵件將被發送。
$secret = "<--secret key-->";
$user_ip = $_SERVER['REMOTE_ADDR'];
if(isset($_POST['g-recaptcha-response'])){
$response = $_POST['g-recaptcha-response'];
// echo "GOT response:".$response."<br/>";
}
else{
// echo "No reCAPTCHA response.<br/>";
}
//Verify response data
$verifyResponse = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$user_ip");
$responseData = json_decode($verifyResponse);
if(!$responseData->success){
$error['captcha'] = "Please prove that you're not a robot.";
}
有什么建議可能會出錯嗎?
你忘了添加?
在網址之后
url: "inc/sendEmail.php?",
或者
你可以省略?
並發送您的數據,例如
data: { this: this, that: that, another: another},
此外,由於您沒有使用表單將g-recaptcha-response
到 php 文件,而是使用 AJAX,您必須手動將帖子發送到您的 php 文件。
var g-recaptcha-response= $('#contactForm .g-recaptcha-response').val();
var data = 'contactName=' + contactName + '&contactEmail=' + contactEmail +
'&contactSubject=' + contactSubject + '&contactMessage=' + contactMessage + '&g-recaptcha-response=' + g-recaptcha-response;
你也需要這個來確認php中的recaptcha
require_once('recaptchalib.php');
$privatekey = "your_private_key";
$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 {
// Your code here to handle a successful verification
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.