我正在使用Googles reCaptcha API进行表单验证。
使用一点JS后,我已选择在验证完成后显示submit
按钮。
<?php
if(isset($_POST['Login'])){
$url = 'https://www.google.com/recaptcha/api/siteverify';
$privatekey = '6LerNA0UAAAAAEReb9rS5JXjtvNSYlMjKiocUv_O';
$response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response);
if(isset($data->success) AND $data->success==true){
//show submit button
echo '<script type=\"text/javascript\">
function myFunction() {
document.getElementById("logDiv").style.visibility="visible";
}
</script>';
}
else{ // stay hidden
'<script type=\"text/javascript\">
function myFunction() {
document.getElementById("logDiv").style.visibility="hidden";
}
</script>';
}
}
?>
<div id='logDiv' style='visibility:hidden')
<?php
echo $form->add('Login',array('type' => 'submit'));
?>
</div>
目前,该解决方案尚未完成;当验证验证码时,div仍然是隐藏的。
这是语法错误的结果还是产生了逻辑错误?
我的代码中有什么错误?
还有更强大的解决方案吗?
答案 0 :(得分:0)
为什么不简单地使用php条件来显示div?我认为你的JS不起作用,因为你从不调用myFunction()。
尝试这样的事情,但随着时间和代码量的增加会变得复杂:
if(isset($data->success) AND $data->success==true){
$showButton = true;
}
......
if($showButton) { ?>
<div id='logDiv' style='visibility:hidden'
<?php echo $form->add('Login',array('type' => 'submit'));
?> </div> <?php }
......
或简单的解决方案:
if(is_bool($data -> success) && $data -> success){
echo '<div id="logDiv">'.$form->add('Login',array('type' => 'submit')).'</div>';
}
只有在验证成功的情况下才回显HTML元素,否则只是不输入Div的任何HTML。
希望这有助于。