1

am trying to validate recaptcha with ajax, the only problem am facing is that it always sais that recapthca was typed wrong!even when i did it right! i really wonder what is wrong with my code!

<?php
    require_once('recaptcha/recaptchalib.php');
    define("PUBLICKEY"," ");
    define("PRIVATEKEY"," ");

    $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
    if ($resp->is_valid) {
    ?>success<?
        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $reason = $_POST['reason'];
        $header = 'From: ' . $email . " \r\n";
        $msg = "Sent from: " . $name . "\r\n";
        $msg .= "Email: " . $email . " \r\n";
        $msg .= "Phone: " . $phone . " \r\n";
        $msg .= "Contact reason:" . $reason . " \r\n";
        $msg .= "Message: " . $_POST['message'] . " \r\n";
        $msg .= "Date and time " . date('d/m/Y', time());

        $to = '';
        $subject = 'contact page';

        mail($to, $subject, utf8_decode($msg), $header);
    }
    else 
    {
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
       "(reCAPTCHA said: " . $resp->error . ")");
    }
?>

on the form page i have this

<?php 
    require_once('recaptcha/recaptchalib.php');
    define("PUBLICKEY","");
    define("PRIVATEKEY"," ");
?>

<div id="contact-form">
<?php echo  $content; ?>
        <form action="#" method="POST" id="contactForm" onSubmit="return validateCaptcha()">
    <div class="form">
            <label for="name">Your Name: <span class="requireds">(Required)</span><br /></label>
            <input id="name" name="name" class="text-input" minlength="2" />
   </div>
   <div class="form">
            <label for="email">Your Email:<span class="requireds">(Required)</span><br /></label>
            <input id="email" name="email" class=" text-input" />
   </div>
   <div class="form">
            <label for="phone">Your Phone:<br /></label>
            <input id="phone" name="phone" type="text"  maxlength="200" class="text-input"  />
   </div>
   <div class="form">
            <label  for="reason">Contact reason:<br /></label>
            <select id="reason" name="reason" class="select">
                <option>Sales question </option>
                <option>Time/ Delivery</option>
                <option>My existing Order</option>
                <option>Technical Question</option>
                <option>Revision/ Support</option>
                <option>Other</option>

            </select>
   </div>
   <div class="form">
            <label for="message">Message: <span class="requireds">(Required)</span> <br /></label>
             <textarea id="message" name="message" class="textarea"></textarea>
   </div>

   <div style="margin:10px 0; width:495px;  -moz-border-radius:3px; border-radius:3px;">
                <p style="color: #f14444; text-align:right; font-size:12px" id="captchaStatus">&nbsp;</p>
                    <?php echo recaptcha_get_html(PUBLICKEY); ?>

      </div>
       <input type="submit"  value="" class="send"/>

    </form> 

i already checked if the public and private key are correct.. does somebody have an idea about what is wrong on this code?

Samir Talwar
  • 14,220
  • 3
  • 41
  • 65
karlelizabeth
  • 157
  • 2
  • 3
  • 13

1 Answers1

0

You are receiving this error when submitting the recaptcha which tells me that your API key is not being submitted properly:

To use reCAPTCHA you must get an API key from https://www.google.com/recaptcha/admin/create

Update:

recaptcha_check_answer ($privatekey,

See the problem there? You're using $privatekey instead of PRIVATEKEY so you're actually submitting nothing as your private key. The error was correct (trust the error messages!). I use Chrome's built in developer tools (Ctrl+Shift+i) to debug this.

Joe Phillips
  • 49,743
  • 32
  • 103
  • 159