View Full Version : Problem Adding a Math Captcha to a Contact Form

03-19-2017, 12:04 AM

I added a math captcha to a contact form and below is all the code that relates to it.

The problem is that the mail gets sent even if the captcha answer is not right.

Please tell me what's wrong?

TEST PAGE (http://www.modularconceptseast.com/index-test.html#contact)

Contact Form


<div class="done">
<h3>Thank you for contacting us! Your message was sent.</h3>
<div class="form">

<form class="validate-form" method="post" action="process-TEST.php">

<div class="span6">
<input type="text" name="name" class="text" placeholder="Name *" style="width:93.7%"/>
<div class="span6"><input type="text" name="email" class="text" placeholder="Email *" style="width:93.7%"/></div>
<!--<div class="span4"><input type="text" name="website" class="text" placeholder="Website (Not Required)" style="width:90%"/></div>-->
<div class="span12">
<textarea name="comment" class="text textarea" placeholder="Message *" style="width :97%; height:97px;"></textarea></div>

<div class="span12 center">
<span style="color:#000">To stop spam-bots, please answer what is <span id="no1"></span> plus <span id="no2"></span>?</span>
<input style="width:12% !important;" type="text" class="text" name="answer" id="answer" pattern="7" title="Please Stop Spam Bots - Resolve The Answer" placeholder="Your Answer *" required>

<div><center><input type="submit" class="btn btn-info btn-large" value="SEND" id="submit"></center></div>


<span id="status" style="display:none">Unsubmitted</span>
<script src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/captcha.js"></script>


//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];
$answer = ($_GET['answer']) ?$_GET['answer'] : $_POST['answer'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;

//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$comment) $errors[count($errors)] = 'Please enter your message.';
if (!$answer) $errors[count($errors)] = 'Please enter your answer.';

//if the errors array is empty, send the mail


$(document).ready(function() {

//if submit button is clicked
$('#submit').click(function () {

//Get the data from all the fields
var name = $('input[name=name]');
var email = $('input[name=email]');
var website = $('input[name=website]');
var answer = $('input[name=answer]');
var comment = $('textarea[name=comment]');

//Simple validation to make sure user entered something
//If error found, add hightlight class to the text field
if (name.val()=='') {
return false;
} else name.removeClass('hightlight');

if (email.val()=='') {
return false;
} else email.removeClass('hightlight');

if (comment.val()=='') {
return false;
} else comment.removeClass('hightlight');

if (answer.val()=='') {
return false;
} else answer.removeClass('hightlight');

//organize the data properly
var data = 'name=' + name.val() + '&email=' + email.val() + '&answer=' + answer.val() +'&comment=' + encodeURIComponent(comment.val());

//disabled all the text fields

//show the loading sign

//start the ajax
//this is the php file that processes the data and sends mail
url: "process-TEST.php",

//GET method is used
type: "GET",

//pass the data
data: data,

//Do not cache the page
cache: false,

success: function (html) {
//if process-TEST.php returned 1/true (send mail success)
if (html==1) {
//hide the form

//show the success message

//if process-TEST.php returned 0/false (send mail failed)
} else alert('Sorry, unexpected error. Please try again later.');

//cancel the submit button default behaviours
return false;