When using reCaptcha, I had a problem. In code, using AJAX to submit the form. Before sending, I need to check whether the fields are filled or not. If the fields are empty, submit should not occur.
In this case, if the text fields are not filled, it displays a warning.
The problem is that after rejecting an invalid record, the submit button stops working for 2 or 3 minutes, and there is no error given by reCaptcha. After a certain period of time, reCaptcha starts working again and the submit button starts working.
<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>
Javascripts:
function sendData(){
console.log('send data - ');
$("#contact-form").submit();
};
$('#contact-form').on("submit", function() {
console.log('clicked submit');
name = $('#name').val().replace(/<|>/g, ""),
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());
$.ajax({
type: "POST",
url: "SaveContact.php",
data: "email=" + email + "&name=" + name + "&msg=" + msg + "&g-recaptcha-response=" + grecaptcha.getResponse()
}).done(function(status) {
if (status == "ok") {
$('#name').val('');
$('#email').val('');
$('#message').val('');
}
});
}});
source
share