2017-07-20 70 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

你不能從一個異步回調函數返回任何 – 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); 

} 
?> 
相關問題