【发布时间】:2017-07-31 06:11:30
【问题描述】:
我不知道如何将它应用到我的登录页面,一旦验证码在 ajax 上成功响应,然后提交表单。 这是我的 html 表单(我将 action 设为 null,因为我仍在测试中)
<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>
这是我如何理解 ajax 在验证码发送验证码字.. 如果验证码 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 来完成 提前谢谢你:)
更新
我只是注意到我怎么能$_POST['g-recaptcha-response']; ??
【问题讨论】:
-
在 SO 上查看这个类似的问题。 stackoverflow.com/questions/31342949/…
标签: php jquery ajax forms recaptcha