使用reCaptcha時,我遇到了問題。在代碼中,使用AJAX提交表單。在提交之前,我需要檢查字段是否填滿。如果字段未填寫,則不應發生提交。reCAPTCHA隱形 - 重新提交表單問題
在這種情況下,如果textfields沒有填充,它會發出警報。
問題出在無效帖子被拒絕後,提交按鈕停止工作2到3分鐘,reCaptcha沒有給出錯誤。經過一段時間後,reCaptcha再次開始工作並提交按鈕。
<form id="contact-form" method="post" action="javascript:void(0)">
<input type="text" placeholder="Name" id="name">
<input type="text" placeholder="E-Mail" id="email">
<textarea placeholder="Message" id="message">/textarea>
<button class="g-recaptcha pull-right" data-sitekey="#your-site-key" data-callback="sendData" type="submit"> SEND <i class="flaticon-origami34"></i> </button>
</form>
的Javascript:
function sendData(){
console.log('send data - '); // --> works
//send datas:
$("#contact-form").submit();
};
$('#contact-form').on("submit", function() {
console.log('clicked submit'); // --> works
name = $('#name').val().replace(/<|>/g, ""), // prevent xss
email = $('#email').val().replace(/<|>/g, ""),
msg = $('#message').val().replace(/<|>/g, "");
if (name == '' || email == '' || msg == '') {
alert("Please fill all areas !");
} else {
console.log('captcha response: ' + grecaptcha.getResponse()); // --> captcha response:
// ajax to the php file to send the mail
$.ajax({
type: "POST",
url: "SaveContact.php",
data: "email=" + email + "&name=" + name + "&msg=" + msg + "&g-recaptcha-response=" + grecaptcha.getResponse()
}).done(function(status) {
if (status == "ok") {
// clear the form fields
$('#name').val('');
$('#email').val('');
$('#message').val('');
}
});
}});
那麼有什麼不工作? –
不工作,2或3分鐘。在那之後工作。 –