I have been trying to figure out how to show a error message (Please try captcha again) in a div with php.
Here is the HTML page:
<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>
Now here is the php page with code to verify captcha is either false or true. I have tested this and it does work if captcha isn't entered it redirects to a page showing the message and if is verity (true) will send email.
This is the part that verity the captcha response false or true: PHP Page:
<?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)
}
Everything works right when I fill out form and recaptcha and click send , email is sent. Now when I don't click on recaptcha box and hit send it close's page but shows a page with the text You are a bot! Go away! I know that is what ECHO does, except I don't want that, I would like to be able to put the error message in the errorMessage div on the HTML page. I have tried $err_message = 'Recaptcha Failed. Please try again' and echo $err_message = 'Recaptcha Failed. Please try again' except neither one of these works either. I am also using a js file to validate the 3 fields, name, email and comment. Any help on this would be great. Have searched all over the internet and haven't come across a solution yet.