Решение, как подключить капчу от Google reCAPTCHA v2 для AJAX формы
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src='https://www.google.com/recaptcha/api.js?hl=ru'></script>
$("#wdh_form").submit(function(e){ e.preventDefault(); $.ajax({ type: "POST", url: "wdh_send_form.php", data: $("#wdh_form").serialize(), success: function(data){ $('#wdh_result_form').html(data); } }); });
<form id="wdh_form">
<div class="g-recaptcha" data-sitekey="_____КЛЮЧ_____"></div>
<div id="wdh_result_form"></div>
require_once($_SERVER["DOCUMENT_ROOT"].'/recaptchalib-v2.php'); $secret = "_____СЕКРЕТНЫЙ_КЛЮЧ_____"; $resp = null; $error = null; $reCaptcha = new ReCaptcha($secret); // Was there a reCAPTCHA response? if ($_POST["g-recaptcha-response"]) { $resp = $reCaptcha->verifyResponse( $_SERVER["REMOTE_ADDR"], $_POST["g-recaptcha-response"] ); } if($resp != null && $resp->success) { echo "reCAPTCHA пройдена!<br>Отправляем форму!"; echo $_POST['text']; } else { echo "Ошибка reCAPTCHA"; }