Jump to content

Need help with my php Re-Captcha Error Message placement please!


tomkilbourn

Recommended Posts

Hello, I have just started to use WordPress to build a site, and have adding a php contact form which incorporates Re-Captcha security.

The Re-Captcha it is working fine, but the error message which is displayed when the Re-Captcha is typed incorrectly is displaying at the very top of the page and I would like the error message "The reCAPTCHA failed with this message:...."  to be displayed next to the Re-Captcha box itself at the bottom of the form.

How would I go about achieving this? I admit I am no good with PHP and have been away from all this for some time!!

<?php 
/* 
Template Name: Contact 
*/ 
?> 

<?php 

// 
require_once('recaptchalib.php');  
$publickey = "xxxxxxxx"; // you got this from the signup page   
$privatekey = "xxxxxxxxx";  
//     

if(isset($_POST['submitted'])) { 
    if(trim($_POST['contactName']) === '') { 
        $nameError = 'Please enter your name.'; 
        $hasError = true; 
    } else { 
        $name = trim($_POST['contactName']); 
    } 

    if(trim($_POST['email']) === '')  { 
        $emailError = 'Please enter your email address.'; 
        $hasError = true; 
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) { 
        $emailError = 'You entered an invalid email address.'; 
        $hasError = true; 
    } else { 
        $email = trim($_POST['email']); 
    } 

    if(trim($_POST['comments']) === '') { 
        $commentError = 'Please enter a message.'; 
        $hasError = true; 
    } else { 
        if(function_exists('stripslashes')) { 
            $comments = stripslashes(trim($_POST['comments'])); 
        } else { 
            $comments = trim($_POST['comments']); 
        } 
    } 
     
    $response = recaptcha_check_answer ($privatekey,  
$_SERVER["REMOTE_ADDR"],  
$_POST["recaptcha_challenge_field"],  
$_POST["recaptcha_response_field"]);  
         
        if( ! $response->is_valid ) {         

            echo '<div id="recaptcha_error_box">The reCAPTCHA failed with this message: '.$response->error.'<br />Please try again.</div>'; 
            // by default now, let the flow-of-control fall back into the <form> below so the user can try again 
         
        } 
         
        else { 

    if(!isset($hasError)) { 
        $emailTo = get_option('tz_email'); 
        if (!isset($emailTo) || ($emailTo == '') ){ 
            $emailTo = get_option('admin_email'); 
        } 
        $subject = '[PHP Snippets] From '.$name; 
        $body = "Name: $name \n\nEmail: $email \n\nComments: $comments"; 
        $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; 

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

}  

} 

?> 
<?php get_header(); ?> 
    <div id="container"> 
        <div id="content"> 

            <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
            <div <?php post_class() ?> id="post-<?php the_ID(); ?>"> 
                <h1 class="entry-title"><?php the_title(); ?></h1> 
                    <div class="entry-content"> 
                        <?php if(isset($emailSent) && $emailSent == true) { ?> 
                            <div class="thanks"> 
                                <p>Thanks, your email was sent successfully.</p> 
                            </div> 
                        <?php } else { ?> 
                            <?php the_content(); ?> 
                            <?php if(isset($hasError) || isset($captchaError)) { ?> 
                                <p class="error">Sorry, an error occured.<p> 
                            <?php } ?> 

                        <form action="<?php the_permalink(); ?>" id="contactForm" method="post"> 
                            <ul class="contactform"> 
                            <li> 
                                <label for="contactName">Name:</label> 
                                <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" /> 
                                <?php if($nameError != '') { ?> 
                                    <span class="error"><?=$nameError;?></span> 
                                <?php } ?> 
                            </li> 

                            <li> 
                                <label for="email">Email</label> 
                                <input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="required requiredField email" /> 
                                <?php if($emailError != '') { ?> 
                                    <span class="error"><?=$emailError;?></span> 
                                <?php } ?> 
                            </li> 

                            <li><label for="commentsText">Message:</label> 
                                <textarea name="comments" id="commentsText" rows="20" cols="30" class="required requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea> 
                                <?php if($commentError != '') { ?> 
                                    <span class="error"><?=$commentError;?></span> 
                                <?php } ?> 
                            </li> 
<li> 
<?php echo recaptcha_get_html($publickey, $errors); ?>   
</li> 
                            <li> 
                                <input type="submit">Send email</input> 
                            </li> 
                        </ul> 
                        <input type="hidden" name="submitted" id="submitted" value="true" /> 
                    </form> 
                <?php } ?> 
                </div><!-- .entry-content --> 
            </div><!-- .post --> 

                <?php endwhile; endif; ?> 
        </div><!-- #content --> 
    </div><!-- #container --> 

<?php get_sidebar(); ?> 
<?php get_footer(); ?> 

 

Simple, change this line echo to a variable $captchaError.  Also you forgot to set the $hasError in the captcha check if()

$captchaError =  '<div id="recaptcha_error_box">The reCAPTCHA failed with this message: '.$response->error.'<br />Please try again.</div>';
$hasError = TRUE;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.