Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Where to find/How to create a Question Captcha Re: Where to find/How to create a Question Captcha

#67819
jandreoni
Member

Hey, Guys–

I am using Baylor’s code from above for this.

Not getting the validation to recognize an incorrect answer. I’m also not getting a display message saying that my answer is incorrect. I am getting the email, though, when I hit submit I’ll past my complete PHP code first, then break down what is new and what I had before. Perhaps it’s my ordering? I am using the jQuery Validation plug-in to validate the form client-side. My validation is catching when the field is blank, though.

Do I need to put the 3rd chunk into my HTML near the actual question?

Sorry for being dense on this, and thanks for any help.

Complete code (new stuff with my old stuff):

Code:
array(
‘question’ => ‘1 + 4’,
‘answer’ => ‘5’
),
2 => array(
‘question’ => ‘What is the color of snow’,
‘answer’ => ‘white’
)
);

function create_question() {
global $questions;

$question_id = rand(1, count($questions));

// Will generate something like $questions[1]
$question = $questions[$question_id];

// Store the question id in a session
$_SESSION[‘question_id’] = $question_id;

echo $question[‘question’];
}

function get_answer() {
global $questions;

$q_id = $_SESSION[‘question_id’];

return $questions[$q_id][‘answer’];
}

if( isset($_POST[‘submit’]) ) {
$captcha = $_POST[‘captcha’];

if( $captcha != get_answer() ) {
$error_message[‘captcha’] = “Your Answer is incorrect”;
}
}else {
// Display Form
}

//If the form is submitted
if(isset($_POST[‘submit’])) {

//Check to make sure that the name field is not empty
if(trim($_POST[‘contactname’]) == ”) {
$hasError = true;
} else {
$name = trim($_POST[‘contactname’]);
}

//Check to make sure that the Company field is not empty
if(trim($_POST[‘company’]) == ”) {
$hasError = true;
} else {
$company = trim($_POST[‘company’]);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST[’email’]) == ”) {
$hasError = true;
} else if (!eregi(“^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,4}$”, trim($_POST[’email’]))) {
$hasError = true;
} else {
$email = trim($_POST[’email’]);
}

//Check to make sure that the Budget field is not empty
if(trim($_POST[‘budget’]) == ”) {
$hasError = true;
} else {
$budget = trim($_POST[‘budget’]);
}

//Check to make sure that the Timeline field is not empty
if(trim($_POST[‘timeline’]) == ”) {
$hasError = true;
} else {
$timeline = trim($_POST[‘timeline’]);
}

//Check to make sure comments were entered
if(trim($_POST[‘message’]) == ”) {
$hasError = true;
} else {
if(function_exists(‘stripslashes’)) {
$comments = stripslashes(trim($_POST[‘message’]));
} else {
$comments = trim($_POST[‘message’]);
}
}

//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = ‘[email protected]’; //Put your own email address here
$body = “Name: $name nCompany: $company nEmail: $email nPhone: $phone nWebsite: $website nBudget: $budget nTimeline: $timeline nHear: $hear nComments: $comments”;
$headers = ‘From: JASONANDREONI.COM <'.$emailTo.'>‘ . “rn” . ‘Reply-To: ‘ . $email;
$subject = “JA.COM – REQUEST FOR A FREE QUOTE”;

mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>

First new bit:

Code:
// The numbers are hooks, so we can pull a specific question
$questions = array(
1 => array(
‘question’ => ‘1 + 4’,
‘answer’ => ‘5’
),
2 => array(
‘question’ => ‘What is the color of snow’,
‘answer’ => ‘white’
)
);

function create_question() {
global $questions;

$question_id = rand(1, count($questions));

// Will generate something like $questions[1]
$question = $questions[$question_id];

// Store the question id in a session
$_SESSION[‘question_id’] = $question_id;

echo $question[‘question’];
}

function get_answer() {
global $questions;

$q_id = $_SESSION[‘question_id’];

return $questions[$q_id][‘answer’];
}

Second new bit:

Code:
if( isset($_POST[‘submit’]) ) {
$captcha = $_POST[‘captcha’];

if( $captcha != get_answer() ) {
$error_message[‘captcha’] = “Your Answer is incorrect”;
}
}else {
// Display Form
}

My original code:

Code:
//If the form is submitted
if(isset($_POST[‘submit’])) {

//Check to make sure that the name field is not empty
if(trim($_POST[‘contactname’]) == ”) {
$hasError = true;
} else {
$name = trim($_POST[‘contactname’]);
}

//Check to make sure that the Company field is not empty
if(trim($_POST[‘company’]) == ”) {
$hasError = true;
} else {
$company = trim($_POST[‘company’]);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST[’email’]) == ”) {
$hasError = true;
} else if (!eregi(“^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,4}$”, trim($_POST[’email’]))) {
$hasError = true;
} else {
$email = trim($_POST[’email’]);
}

//Check to make sure that the Budget field is not empty
if(trim($_POST[‘budget’]) == ”) {
$hasError = true;
} else {
$budget = trim($_POST[‘budget’]);
}

//Check to make sure that the Timeline field is not empty
if(trim($_POST[‘timeline’]) == ”) {
$hasError = true;
} else {
$timeline = trim($_POST[‘timeline’]);
}

//Check to make sure comments were entered
if(trim($_POST[‘message’]) == ”) {
$hasError = true;
} else {
if(function_exists(‘stripslashes’)) {
$comments = stripslashes(trim($_POST[‘message’]));
} else {
$comments = trim($_POST[‘message’]);
}
}

//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = ‘[email protected]’; //Put your own email address here
$body = “Name: $name nCompany: $company nEmail: $email nPhone: $phone nWebsite: $website nBudget: $budget nTimeline: $timeline nHear: $hear nComments: $comments”;
$headers = ‘From: JASONANDREONI.COM <'.$emailTo.'>‘ . “rn” . ‘Reply-To: ‘ . $email;
$subject = “JA.COM – REQUEST FOR A FREE QUOTE”;

mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}