Php 未提交验证码时的警告标志
我有一个表格,上面有一个谷歌验证码v2勾选框。基本上,我希望表格不提交,除非已填写验证码。目前,如果填写的表格没有验证码,表格将与您没有填写验证码的表格相呼应;将显示在新页面上 但是,我希望它保持在同一页上,并发出警告,防止他们提交,这样他们就不会丢失他们在表单中所写的内容 用于检查验证码PHP的脚本:Php 未提交验证码时的警告标志,php,html,css,Php,Html,Css,我有一个表格,上面有一个谷歌验证码v2勾选框。基本上,我希望表格不提交,除非已填写验证码。目前,如果填写的表格没有验证码,表格将与您没有填写验证码的表格相呼应;将显示在新页面上 但是,我希望它保持在同一页上,并发出警告,防止他们提交,这样他们就不会丢失他们在表单中所写的内容 用于检查验证码PHP的脚本: if(isset($_POST['g-recaptcha-response'])){ $captcha=$_POST['g-recaptcha-response']; } if(!$capt
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h1>You did not fill in the CAPTCHA<h1>';
exit;
}
$secretKey = "my_secret_key";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
if($responseKeys["success"]) {
if(!$mail->Send()) {
echo "mail sent failed";
}
else {
header('Location: ../inquire.php?success=true');
}
} else {
echo '<h2>You are spammer ! Get the @$%K out</h2>';
}
申请我们的服务
发送电子邮件
试试这个
<!DOCTYPE html>
<html lang="en">
<head>
<script src='https://www.google.com/recaptcha/api.js' async defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
</head>
<body>
<form method="post" role="form" class="contact1-form validate-form" id="loginForm">
<span class="contact1-form-title">
Apply for our service
</span>
<div class="wrap-input1 validate-input" data-validate="Full name is required">
<input class="input1" type="text" name="name" id="name" placeholder="Full name">
<span class="shadow-input1"></span>
</div>
<div class="wrap-input1 validate-input" data-validate="Valid email is required: ex@abc.xyz">
<input class="input1" type="text" name="email" id="email" placeholder="Email">
<span class="shadow-input1"></span>
</div>
<div class="wrap-input1 validate-input" data-validate="Social media handles are required: @johnsmith">
<input class="input1" type="text" name="subject" id="subject" placeholder="Social media handles">
<span class="shadow-input1"></span>
</div>
<div class="wrap-input1 validate-input" data-validate="Message is required">
<textarea class="input1" name="message" id="message" placeholder="Tell us about yourself"></textarea>
<span class="shadow-input1"></span>
</div>
<div class="g-recaptcha" data-sitekey="my_site_key"></div>
<br>
<div class="container-contact1-form-btn">
<button class="contact1-form-btn" type="submit">
<span>
Send Email
<i class="fa fa-long-arrow-right" aria-hidden="true"></i>
</span>
</button>
</div>
</form>
</body>
</html>
PHP
JS
您必须使用jQueryAjax验证captcha,就像如果不太麻烦的话,您可以发布一个我可以在代码脚本中实现的答案吗@PaulBaijuokey,我已经回答了,检查它是否解决了你的问题我试过了。我更改了脚本的PHP,然后更改了脚本的HTML。然后,我用HTML inquire.php将JS添加到主文件的head中。然而,这并没有起到任何作用。当我尝试使用该表单时,我尝试使用已完成的验证码,但它没有发送电子邮件;当我尝试不使用验证码时,它重定向回页面,但什么也没有发生@PaulBaijuNo,只要我填写验证码,它就会重定向到,而不会发送电子邮件。但是,如果我不填写验证码,它会重定向到,并且会nothing@slkdanfalvsd如果您填写CAPTCHA,它应该重定向到mywebsite.com/inquire.php?success=true。正当问题中未提及发送电子邮件部分,也未给出其代码。
<?php
if(isset($_POST['submit']) && !empty($_POST['submit'])){
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
$secretKey = "my_secret_key";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
if($responseKeys["success"]) {
if(!$mail->Send()) {
echo "mail sent failed";
}
else {
header('Location: ../inquire.php?success=true');
}
} else {
echo '<h2>You are spammer ! Get the @$%K out</h2>';
}
}else{
echo '<h1>You did not fill in the CAPTCHA<h1>';
}
}else{
//Nothing
}
?>
<script>
$(document).ready(function() {
var loginForm = $("#loginForm");
//We set our own custom submit function
loginForm.on("submit", function(e) {
//Prevent the default behavior of a form
e.preventDefault();
//Get the values from the form
var name = $("#name").val();
var email = $("#email").val();
var subject = $("#subject").val();
var message = $("#message").val();
//Our AJAX POST
$.ajax({
type: "POST",
url: "service.php",
data: {
name: name,
email: email,
subject: subject,
message: message,
//This will tell the form if user is captcha varified.
gresponse: grecaptcha.getResponse()
},
success: function(response) {
console.log(response);
//console.log("Form successfully submited");
}
})
});
});
</script>