2017-07-20 3 views
-1

私は、recaptchaがfalseを返す場合にフォームを停止しようとしていますが、それでもフォームは送信されます。私もe.preventDefaultを使用していますが、それは動作していないので、正しくないものでなければなりません。recaptcha 2はfalseを返しますが、依然としてfomを送信します

ご協力いただきありがとうございます。

のjQuery:

submit: function() { 
$form = $('#registration-form'); 
$form.submit(function (e) { 
$output = _validate($form); 
    if($output == false) { 
    e.preventDefault(e); 
    } else { 
    $.ajax({ 
     type: "POST", 
     url: "http://" + window.location.hostname + "/wp-content/themes/Listex/includes/plugins/forms/recaptcha.php", 
     async: false, 
     data: "g-recaptcha-response=" + grecaptcha.getResponse(), 
     success: function(response) { 
     alert(response); 
     if (response == "false") 
      return false; 
     } 
    }); 
} 
}); 

は}、

recaptcha.php:

<?php 

$secret="secret code"; 
$response = $_POST["g-recaptcha-response"]; 

$verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}"); 
$captcha_success = json_decode($verify); 

echo json_encode($captcha_success->success); 
+0

あなたは、私がstackoverflowの上で見て、まだないと動作しませんしようとした何かのthats申し訳ありません、非同期コールバック関数 – geocodezip

+0

から何かを返すことはできません – Max

答えて

0

端でソートは、現在の検証とそれを連結:

のjQuery:

if(output == true) { 
    $.ajax({ 
     type: "POST", 
     async: false, 
     url: "http://" + window.location.hostname + "/wp-content/themes/Listex/includes/plugins/forms/recaptcha.php", 
     data: { 
     //THIS WILL TELL THE FORM IF THE USER IS CAPTCHA VERIFIED. 
     captcha: grecaptcha.getResponse() 
     }, 
     success: function(data) { 
     console.log("FORM SUCCESSFUL"); 
     output = data; 
     }, 
    }); 

    } 

    if (!output) alert('Please ensure all required fields have been entered'); 

    return output; 

recaptcha.php:

<?php 

if(isset($_POST['captcha']) && !empty($_POST['captcha'])){ 

$secret="6Lej1CkUAAAAAP0ACyvliPJo7Tp5I2eH52C-rsfG"; 
$response = $_POST["captcha"]; 

$verify = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}"); 
$captcha_success = json_decode($verify); 

echo json_encode($captcha_success->success); 

} 
?> 
関連する問題