Help please solve the problem. There is an HTML feedback form with ajax request. I can not screw reCAPTCHA to it. Without ajax, everything works fine (the Form sends a letter only in the case of validation of a captcha), but with ajax it does not work, it just goes without a captcha

Ajax request

$(document).ready(function() { $("#feedback-form").submit(function() { $.ajax({ type: "POST", url: "send.php", data: $(this).serialize() }).done(function() { $(this).find("input").val(""); $("#feedback-form").trigger("reset"); document.getElementById('results' ).style.display = 'block'; }); return false; }); 

});

Php handler

  if($_SERVER['REQUEST_METHOD'] == 'POST') { if (empty($_POST['g-recaptcha-response'])) { exit('Empty'); } $url = 'https://www.google.com/recaptcha/api/siteverify'; $recaptcha = $_POST['g-recaptcha-response']; $secret = 'мой_секретный_ключ'; $ip = $_SERVER['REMOTE_ADDR']; $url_data = $url.'?secret='.$secret.'&response='.$recaptcha.'&remoteip='. $ip; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url_data); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($curl); curl_close($curl); $res = json_decode($res); if($res->success) { echo 'YES'; } else { exit('Error'); } } if((isset($_POST['name'])&&$_POST['name']!="") &&(isset($_POST['email'])&&$_POST['email']!="") &&(isset($_POST['message'])&&$_POST['message']!="")) { $to = "почта"; $name = trim($_POST["name"]); $email = trim($_POST["email"]); $message = trim($_POST["message"]); $message = "Имя: $name \nEmail: $email \nСообщение: $message"; $pagetitle = "Новое сообщение"; mail($to, $pagetitle, $message, "Content-type: text/plain; charset=\"utf-8\"\n From: $email"); } 
  • Try sending the sending process here in this condition if ($ res-> success) {echo 'YES'; } - zkolya
  • Yes, it works that way, thanks (the message comes to the mail itself only if the captcha is checked). But how to do ajax validation before sending to the handler? If in case captcha is omitted, then asks to enter it. - Vladislav
  • In fact, as I understand it, you have already implemented it, you just need to work with the else condition if the captcha is not passed. Try experimenting with echo, then in js receive the response from the server and work with it - zkolya
  • Yes, I try, but so far something does not come out :( - Vladislav
  • @Vladislav In the moss through the console, look for a start, what data is left by the post method using ajax . Is there a g-recaptcha-response with a value? Your question is simple, I can help without any problems! Yesterday I just helped to solve the issue on the same captcha. Link to stackoverflow - Dmitry

1 answer 1

You can get the key with grecaptcha.getResponse ();

 $(document).ready(function() { $("#feedback-form").submit(function() { var _formData = $(this).serialize(); _formData['g-recaptcha-response'] = grecaptcha.getResponse(); $.ajax({ type: "POST", url: "send.php", data: _formData }).done(function() { $(this).find("input").val(""); $("#feedback-form").trigger("reset"); document.getElementById('results' ).style.display = 'block'; }); return false; });