• I have a form that works beautifully. I’ve started getting loads of spam so decided to add reCaptcha to fix this. The client actually asked for reCaptcha specifically so it’s got to be reCaptcha. The forms are hardcoded into my theme’s template files (not added with a plugin). The problem is I can’t get reCaptcha to work in the page template files. I’ve followed the tutorials available however wherever I enter the ‘require_once’ piece of code (see snippet) within my template is where the code stops being output.

    <script type="text/javascript">
     var RecaptchaOptions = {
        theme : 'blackglass'
     };
     </script>
    <form class="enquiry" action="/private/contact-response.php" method="post" onsubmit="return formValidator()">
    		<ul class="plain-list">
    			<li>
    			<label for="name">Name</label>
    			<input id="name" name="name" class="text" type="text" />
    			</li>
    
    			<li>
    			<label for="email">Email address</label>
    			<input id="email" name="email" class="text" type="text" />
    			</li>
    
    			<li>
    			<label for="phone">Telephone number</label>
    			<input id="phone" name="phone" class="text" type="text" />
    			</li>
    			<li>
    			<select name="typeofenquiry" id="typeofenquiry">
    			<option value=" Type of booking enquiry " class="head-option">Type of booking enquiry</option>
    			<option value=" Surfing Lessons ">Surfing Lessons</option>
       		 	<option value=" Quest Coasteering ">Quest Coasteering</option>
       		 	<option value=" Equipment Hire ">Equipment Hire</option>
    			<option value=" Roxy Girl Days ">Roxy Girl Days</option>
    			<option value=" Shopping Enquiry ">Shopping Enquiry</option>
    			<option value=" Stag & Hen Activities ">Stag & Hen Activities</option>
    			<option value=" General Enquiry ">General Enquiry</option>
    
    			</select>
    			</li>
    			<li>
    			<textarea id="message" name="message" class="text" onclick="if(this.value == 'Additional Info') this.value='';" onblur="if(this.value.length == 0) this.value='Additional Info';" rows="" cols="" >Additional Info</textarea>
    </li>
    
    <li><?php
    require_once('/private/recaptchalib.php');
    $publickey = "xxxxxxxxxxxxxxxxxxx"; // you got this from the signup page
    echo recaptcha_get_html($publickey);
    ?>
      </li>
    			<li style="clear:both;">
    			<input class="send" type="submit" value="Submit Enquiry" />
    			</li>
    
    			</ul>
    			</form>

    Then there’s the code in contact-response.php …

    <?php
     require_once('/private/recaptchalib.php');
     $privatekey = "xxxxxxxxxxxxxxxx";
     $resp = recaptcha_check_answer ($privatekey,
                                     $_SERVER["REMOTE_ADDR"],
                                     $_POST["recaptcha_challenge_field"],
                                     $_POST["recaptcha_response_field"]);
     if (!$resp->is_valid) {
       // What happens when the CAPTCHA was entered incorrectly
       die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
            "(reCAPTCHA said: " . $resp->error . ")");
     } else {
       // Your code here to handle a successful verification
     }
     ?>
     <?php
    
    //start building the mail string
    
    $msg = "Name:    $_POST[name]\n";
    $msg .= "Email Address: $_POST[email]\n";
    $msg .= "Telephone number: $_POST[phone]\n";
    $msg .= "Type of Enquiry: $_POST[typeofenquiry]\n";
    $msg .= "Message:    $_POST[message]\n";
    
    $thankyoupage = "/contact-thankyou.php";
    
    //set up the mail
    $recipient = "[email protected]";  //the inbox where the sent mail goes.
    $subject = "Website enquiry";
    $mailheaders = "From:  <[email protected]> \n";
    $mailheaders .= "Reply-To: $_POST[email]";
    //send the mail
    mail($recipient, $subject, $msg, $mailheaders);
    
    header("Location: $thankyoupage");
    
    ?>

    Both contact-response.php and recaptchalib.php in are located in ‘private’ folder at root. The page template is obviously in the theme folder. The form and other validation works fine without the reCaptcha code. I look forward to some assistance on this – I’m stuck!

  • The topic ‘Can't get reCaptcha to work in page template.’ is closed to new replies.