gpt4 book ai didi

javascript - reCaptcha/表单验证

转载 作者:行者123 更新时间:2023-12-03 08:06:21 25 4
gpt4 key购买 nike

你好,我想将 reCaptcha(2) 导入到我的注册表单中。我已经导入了,但他们不验证它。单击提交时,跳过空的 reCaptcha 并忽略它。有人可以发布完整的 HTMLJAVA/SCRIPT 代码(使用 php)。我找不到正确的代码。我想要一个完整的 html/脚本代码。 :x

我尝试过这个,但我需要 <script>代码来验证它。我搜索了代码,尝试了一些脚本,但它无法验证 <form> .

<html>
<head>
<title>TEST</title>
<script src='https://www.google.com/recaptcha/api.js?hl=en'></script>
</head>
<body>
<form method="POST" action="register.html">
<input type="text" name="name" required>
<br>
<input type="email" name="email" required>
<br>
<input type="password" name="password" required>
<br>
<center><div class="g-recaptcha" data-theme="dark" data-sitekey="MY_SITE_KEY"></div></center><br>
<br>
<input type="submit" value="submit">
</body>
</html>

谢谢。

最佳答案

由于您必须进行服务器端验证码验证,因此表单的 action 部分应该是 register.php,而不是 register.html.您的 HTML 代码应如下所示:

<form method="POST" action="register.php">
<input type="text" name="name" required><br />
<input type="email" name="email" required><br />
<input type="password" name="password" required><br />
<center>
<div class="g-recaptcha" data-theme="dark" data-sitekey="YOUR_SITE_KEY"></div>
</center><br />
<input type="submit" name="submit" value="submit">
</form>

这就是验证 register.php 文件中输入验证码的方法,

<?php

if(isset($_POST['submit'])){

//your site secret key
$secret = 'XXXXXXX_Secret-key_XXXXXXX';

if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
//get verified response data
$param = "https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$_POST['g-recaptcha-response'];
$verifyResponse = file_get_contents($param);
$responseData = json_decode($verifyResponse);

if($responseData->success){
// success
echo "success";
}else{
// failure
}

}

}

?>

引用资料如下:

已编辑:

根据您的评论,

2x borders on side of . for example then i get a error, it add's a style(border-left:1px solid red;border-right:1px solid red;) or it add's a class(recaptcha-error). When success shows only the reCaptcha green mark withuot a border.

这是解决方案,

你的样式表应该是这样的:

<style>
.error {
border:5px solid red;
}
.success{
border:5px solid blue;
}
</style>

您的 HTML 将保持原样,而您的 jQuery 应如下所示:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('[name="submit"]').click(function(e){
var recaptchaResponse = grecaptcha.getResponse();

if(recaptchaResponse == ""){
$(".g-recaptcha").addClass("error");
}else{
$(".g-recaptcha").addClass("success");
}

});
});
</script>

关于javascript - reCaptcha/表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34372815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com