使用谷歌隐形验证码图标时出现问题



我在我的codeigniter项目中有一个表单,使用谷歌的不可见验证码,如下所示:

.HTML

<html>
<head>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
function onSubmitInvisRecaptcha(token) {
document.getElementById("contact_us-form").submit();
}
</script>
</head>
<body>
<form id="contact_us-form" method="post" action="/info/contact_us">
<div>
<label>full name</label>
<input type="text" name="full_name" value="<?php echo $this->input->post('full_name'); ?>"/>
</div>
<div>
<button type="submit" 
id="submit_btn" 
class="btn my-other-styles g-recaptcha"
data-sitekey="<?php echo $sitekey; ?>" 
data-callback="onSubmitInvisRecaptcha">
Submit
</button>
</div>
</form>
</body>
</html>

.PHP

defined('BASEPATH') OR exit('No direct script access allowed');
class Info extends MY_Controller 
{
function contact_us()
{
print_r($_POST);
}
}

从我的代码中,我有 2 个问题:(我希望可以在 1 个帖子中询问多个问题(

  1. 在页面中找不到验证码图标。我已经检查了我在表单中使用的站点密钥与我在www.google.com/recaptcha/admin中找到的站点密钥相同。

  2. contact_us函数中,print_r($_POST);没有g-recaptcha-response..

PS:表单是使用 ajax 显示的另一个页面的一部分,因此表单被另一个<div>包装。

最后我从这个 SO 答案中找到了答案。该链接在一个页面中显示了多个 recaptcha/表单的代码,但它非常简单,可以根据我的需求进行理解和修改。

基本上,如果我理解正确,我的代码失败的原因是因为以下几点:

  1. 我需要使用单独的<div>元素来应用验证码而不是提交按钮。

  2. 谷歌验证码将在页面加载时尝试找到指定的元素,否则,如果元素仅在某些操作后出现或添加到页面,我需要使用 javascript手动渲染和执行grecaptcha。

最新更新