View Full Version : How to get ReCaptcha message displayed on form after button is pressed?

11-21-2011, 12:53 AM

I am trying to adjust my "contact.php" contact form to allow the use of ReCaptcha.

I am trying to get the message which appears when the typed words don't match or when they do match to appear below the submit button after it is pressed.

I am currently getting the message "The words didnt match. Please try again", when I press the submit button regardless of wether the words do actually match or not.
Here is my code -

$errors = null;

/*Re-captcha code starts here - linked to recaptchalib.php*/
$publickey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // you got this from the signup page
$privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

if (isset($_POST['submit']))
$yourname = $_POST['yourname'];
$email = $_POST['email'];
$message = $_POST['message'];
$response = recaptcha_check_answer ($privatekey,

if ($response->is_valid) {
echo "Yes, that was correct!"; }
else {
# set the error code so that we can display it
echo "The words didnt match. Please try again.";

// code to send email

$to = 'me@me.com';
$subject = 'Email query from me.com';
$message = "Email sent from: $email \n $yourname has sent you a message:- \n $message";
$headers = 'From: queries@me.com' . "\r\n" .
'Reply-To: queries@me.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers) or exit('mail FAIL');
echo '<p>Thank you for emailing me! Your Message has been sent.</p>';
echo '<p>Please re-check your form entries.</P>';

/*Re-captcha code ends here*/


<form id="tomsform" method="POST" action="<?php echo JRoute::_( 'index.php' );?>">

<input type="text" name="yourname" size="19" value="<?php echo (isset($yourname)) ? $yourname : '';?>" class="textbox">
if(isset($errors['yourname'])) echo $errors['yourname'];
<p>Email Address</p>
<input type="text" name="email" size="19" value="<?php echo (isset($email)) ? $email : '';?>" class="textbox">
if(isset($errors['email'])) echo $errors['email'];
<textarea rows="9" name="message" cols="30" class="textbox"><?php echo (isset($message)) ? $message: '';?></textarea>
<?php echo recaptcha_get_html($publickey, $errors); ?>
<input type="submit" value="Submit" name="submit" class="go">