Hello,

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. Could anyone have a look and point me in the right direction please?

:o

Here is my code -


<?php 
	$errors = null; 


/*Re-captcha code starts here - linked to recaptchalib.php*/
require_once('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, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); 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 if(count($errors)==0){ $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>'; } else { 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' );?>"> <p>Name</p> <input type="text" name="yourname" size="19" value="<?php echo (isset($yourname)) ? $yourname : '';?>" class="textbox"> <?php if(isset($errors['yourname'])) echo $errors['yourname']; ?> <br> <p>Email Address</p> <input type="text" name="email" size="19" value="<?php echo (isset($email)) ? $email : '';?>" class="textbox"> <?php if(isset($errors['email'])) echo $errors['email']; ?> <br> <p>Message</p> <textarea rows="9" name="message" cols="30" class="textbox"><?php echo (isset($message)) ? $message: '';?></textarea> <br> <br> <?php echo recaptcha_get_html($publickey, $errors); ?> <input type="submit" value="Submit" name="submit" class="go"> </form> <?php ?>

    in this case,

    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    
    if(count($errors)==0){

    on an invalid validation code, you don't fill $errors array, means your form validation is wrong in all cases!

    instead of just echo the problem, try to add into your $errors array:

    $errors[]= "The words didnt match. Please try again.";

      Along the same line as what djjjozsi pointed out above, note that the object returned also contains an error member variable that contains an error code you can pass along to recaptcha_get_html(). Relevant note from the reCAPTCHA API docs:

      Google wrote:

      If $resp->is_valid is false then the user failed to provide the correct captcha text and you should redisplay the form to allow them another attempt. In this case $resp->error will be an error code that can be provided to recaptcha_get_html. Passing the error code makes the reCAPTCHA control display a message explaining that the user entered the text incorrectly and should try again.

      In other words, you shouldn't be passing recaptcha_get_html() your $errors array but rather the error code string found at $response->error (if an error occurs).

        Write a Reply...