我一直试图找出如何在带有php的div中显示错误消息(请再次尝试验证码)。
以下是HTML页面:
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/bootstrapValidator.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/style.css">
<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="container">
<form class="well form-horizontal" action="contact.php" method="post" id="contact_form">
<fieldset>
<!-- Form Name -->
<legend>Contact Us Today!</legend>
<!-- To Whom Select -->
<div class="form-group">
<label class="col-md-4 control-label">Send To*</label>
<div class="col-md-4 selectContainer">
<div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
<select id="sendTo" name="sendTo" class="form-control selectpicker" >
<option value=" ">Select Who To Email</option>
<option id="sb" value="sb">Stephanie</option>
<option id="mb" value="mb">Michael</option>
<option id="questions" value="questions">Questions</option>
</select>
</div>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label">First Name</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input name="fullname" placeholder="Full Name" class="form-control" type="text">
</div>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label">E-Mail</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
<input name="email" placeholder="E-Mail Address" class="form-control" type="text">
</div>
</div>
</div>
<!-- Text area -->
<div class="form-group">
<label class="col-md-4 control-label">Project Description</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
<textarea class="form-control" name="comment" placeholder="Project Description"></textarea>
</div>
</div>
</div>
<!-- Captcha -->
<div class="form-group">
<label class="col-md-4 control-label">Captcha</label>
<div class="col-md-4 inputGroupContainer">
<div class="g-recaptcha" data-sitekey="SITE KEY #"></div>
</div>
</div>
<!-- Error message -->
<div class="errorMessage"><?php echo $err_message; ?> </div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label"></label>
<div class="col-md-4">
<button type="submit" class="btn btn-warning" >Send <span class="glyphicon glyphicon-send"></span></button>
</div>
</div>
</fieldset>
</form>
</div><!-- /.container -->
<script src="js/jquery-3.1.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrapvalidator.min.js"></script>
<script src="js/index.js"></script>
现在这里是带有代码的php页面,用于验证验证码是假还是真。我已经对此进行了测试,如果没有输入验证码,它确实有效,重定向到显示该消息的页面,如果真实(真实)将发送电子邮件。
这是验证验证码响应为false或true的部分: PHP页面:
<?php
session_start();
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
//Contact Form Data
$fullnameField = $_POST['fullname'];
$emailField = $_POST['email'];
$commentsField = $_POST['comment'];
$secret="SECRET KEY #";
$response=$_POST["g-recaptcha-response"];
$verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
echo "<p>You are a bot! Go away!</p>";
}
else if ($captcha_success->success==true) {
(code to send mail using phpMailer)
}
当我填写表格并重新收到并单击发送,电子邮件发送时,一切正常。现在,当我没有点击recaptcha box并点击发送它关闭的页面,但显示一个页面,其中包含文本你是一个机器人!走开!我知道这就是ECHO所做的,除非我不想要,我希望能够将错误消息放在HTML页面的errorMessage div中。 我试过$ err_message =&#39; Recaptcha失败了。请再试一次&#39; 和echo $ err_message =&#39; Recaptcha失败。请再试一次&#39;除了其中任何一个都没有。我还使用js文件来验证3个字段,名称,电子邮件和评论。对此的任何帮助都会很棒。已经在互联网上搜索过,但还没有找到解决方案。