views:

105

answers:

2

Hi there,

I'm building a contact us page that also uses a reCaptcha, but im having a few issues with it. I fill in all of the fields in the contact form and the correct reCaptcha words, but the form does not submit. I'm assuming this is something to do with the validation, but wondered if someone might be able to spot where i'm going wrong?

The PHP code at the top of my page looks like this;

<?php include('includes/session.php');

$err = '';
$success = '';
if(isset($_POST["docontact"]) && $_POST["docontact"] == "yes") {
    //get form details
    $form = new stdClass();
    $form->name = sanitizeOne($_POST["name"], "str");
    $form->email = sanitizeOne($_POST["email"], "str");
    $form->phone = sanitizeOne($_POST["phone"], "str");
    $form->mysevenprog = sanitizeOne($_POST["mysevenprog"], "str");
    $form->enquiry = sanitizeOne($_POST["enquiry"], "str");
    $form->howfindsite = sanitizeOne($_POST["howfindsite"], "str");

    //Check for errors (required: name, email, enquiry)
    if($form->name == "") {
        $err .= '<p class="warning">Please enter your name!</p>';
    }
    if($form->email == "") {
        $err .= '<p class="warning">Please enter your email address!</p>';
    }
    if($form->enquiry == "") {
        $err .= '<p class="warning">Please supply an enquiry message!</p>';
    }

    //Send Email
    if($err == "") {
        $mailer = new BlueMailer();
        $mailer->AddAddress(Configuration::getVar("developer_email"), Configuration::getVar("admin_email_name"));
        include('templates/email/contact-us-admin.php');
        if(!$mailer->Send()) {
            $err .= "<p>There was an error sending submitting your request!, Please try again later.";
        } else {
            $success = 'thanks';
        }

    }

} else {
    //Initialise empty variables
    $form = new stdClass();
    $form->name = "";
    $form->email = "";
    $form->phone = "";
    $form->mysevenprog = "";
    $form->enquiry = "";
    $form->howfindsite = "";
}
?>

And then in the body of my page I have the form as follows;

                <?php if($err != "") : ?>
                    <div class="error">
                        <?php echo $err; ?>
                    </div>
                <?php endif; ?>
                <?php if($success == 'thanks') : ?>
                <h3>Thank you for your enquiry</h3>
                <p>Your enquiry has been successfully sent. Someone will contact you shortly.</p>
                <?php else: ?>
                <h3>If you are looking to advertise with us, have some feedback about some of our programming or want to say 'Hi' please use the fields below</h3>

                <form name="contactus" id="contactus" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'] ?>">
                    <ul>
                        <li><label for="name">Your name: *</label> <input name="name" id="name" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->name ?>" /></li>
                        <li><label for="email">Email address: *</label> <input name="email" id="email" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->email ?>" /></li>
                        <li><label for="phone">Telephone:</label>   <input name="phone" id="phone" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->phone ?>" /></li>
                        <li><label for="mysevenprog">My Seven programme</label> <input name="mysevenprog" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->mysevenprog ?>" /></li>  
                        <li><label for="enquiry">Enquiry/Message: *</label> <textarea name="enquiry" class="textarea" rows="5" cols="30" style="width: 75%;" id="enquiry"><?php echo $form->enquiry ?></textarea></li>
                        <li><label for="howfindsite">How did you find out about our site?</label> <input name="howfindsite" id="howfindsite" class="textbox" style="width: 75%;" type="text" value="<?php echo $form->howfindsite ?>" /></li>
                        <li>

                        <?php

                        require_once('recaptchalib.php');

                        // Get a key from http://recaptcha.net/api/getkey
                        $publickey = "";
                        $privatekey = "";

                        # the response from reCAPTCHA
                        $resp = null;
                        # the error code from reCAPTCHA, if any
                        $error = null;

                        # was there a reCAPTCHA response?
                        if ($_POST["recaptcha_response_field"]) {
                                $resp = recaptcha_check_answer ($privatekey,
                                                                $_SERVER["REMOTE_ADDR"],
                                                                $_POST["recaptcha_challenge_field"],
                                                                $_POST["recaptcha_response_field"]);

                                if ($resp->is_valid) {
                                        echo "You got it!";
                                } else {
                                        # set the error code so that we can display it
                                        $error = $resp->error;
                                }
                        }
                        echo recaptcha_get_html($publickey, $error);
                        ?>

                        </li>
                        <li><input type="submit" value="Submit Form" class="button" /></li>
                    </ul>
                    <input type="hidden" name="docontact" value="yes" />
                </form>
                <?php endif; ?>

The form gets rendered like this in the browser;

        <form name="contactus" id="contactus" method="post" action="/contact-us2.php"> 
            <ul> 
                <li><label for="name">Your name: *</label> <input name="name" id="name" class="textbox" style="width: 75%;" type="text" value="" /></li> 
                <li><label for="email">Email address: *</label> <input name="email" id="email" class="textbox" style="width: 75%;" type="text" value="" /></li> 
                <li><label for="phone">Telephone:</label>   <input name="phone" id="phone" class="textbox" style="width: 75%;" type="text" value="" /></li> 
                <li><label for="mysevenprog">My Seven programme</label> <input name="mysevenprog" class="textbox" style="width: 75%;" type="text" value="" /></li>  
                <li><label for="enquiry">Enquiry/Message: *</label> <textarea name="enquiry" class="textarea" rows="5" cols="30" style="width: 75%;" id="enquiry"></textarea></li> 
                <li><label for="howfindsite">How did you find out about our site?</label> <input name="howfindsite" id="howfindsite" class="textbox" style="width: 75%;" type="text" value="" /></li> 
                <li> 

                <script type="text/javascript" src="http://api.recaptcha.net/challenge?k=XXXXXXXXXXXXXXXXXXX"&gt;&lt;/script&gt; 

<noscript> 
    <iframe src="http://api.recaptcha.net/noscript?k=XXXXXXXXXXXXXXXXXXXX" height="300" width="500" frameborder="0"></iframe><br/> 
    <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea> 
    <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/> 
</noscript> 
                </li> 
                <li><input type="submit" value="Submit Form" class="button" /></li> 
            </ul> 
            <input type="hidden" name="docontact" value="yes" /> 
        </form>
A: 

Take a look at Using reCAPTCHA to stop spam in PHP.

fire
+1  A: 

Just use Zend_Service_ReCaptcha. You'll integrate this service just with few lines:

//Creating instance
$recaptcha = new Zend_Service_ReCaptcha($pubKey, $privKey);

//Display output
echo $recaptcha->getHTML();

//Handling input
$result = $recaptcha->verify(
    $_POST['recaptcha_challenge_field'],
    $_POST['recaptcha_response_field']
);

//And finally validate captcha
if ($result->isValid()) {
    //Cool!
}
Pawka
Hey man. Will this work if the site is on a Windows Server?
Neil Bradley
Sure. It doesn't matter what kind of server you are running.
Pawka