我不知道如何將這個應用到我的登錄頁面,一旦驗證成功響應Ajax然後提交表單。 這是我的HTML表單(我離開的動作無效,因爲我仍然在測試)谷歌reCaptcha驗證使用jQuery AJAX
<form action = "" method = "post">
<input type = "text" id = "email" name = "email">
<input type = "password" id = "pass" name = "password">
<div class = "form-group col-lg-6">
<div class="g-recaptcha" data-sitekey="MY_KEY"></div>
</div>
<input type = "button" id = "submit" value = "submit">
</form>
這就是我所理解的驗證碼發送的驗證碼字..阿賈克斯如果驗證碼success
提交表單,如果failed
我舉一個alert
。
$('#submit').click(function() {
var captcha = "captcha";
$.ajax({
url: "captcha.php",
method: "post",
data:{captcha:captcha},
success:function(data){
if(data=='success'){
$('form').submit();
}
}
else{
alert('captcha failed. try again');
}
});
});
我captcha.php
我如何收到$_POST['captcha']
<?php
if($_POST['captcha']){
$url = 'https://www.google.com/recaptcha/api/siteverify';
$privatekey = 'MY_SECRET_KEY';
$response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response);
if($data->sucess==true){
echo "success";
}
else{
echo "failed";
}
}
?>
請幫助我瞭解如何將它的工作以及它如何使用AJAX 進行預先感謝您:)
UPDATE
我只是注意到我怎麼能$_POST['g-recaptcha-response'];
??
看到這個類似的問題對SO。 https://stackoverflow.com/questions/31342949/recaptcha-how-to-autosubmit-the-form-when-the-captcha-was-send/31372916#31372916 – colecmc