-2

This is my form code:

echo '
              <div class="ctext" id="form">
                    <table><form method="post" action="prashanja.php">
                    <tr><td>Name:</td><td><input type="text" name="name"/></td></tr>
                   <tr><td>Question:</td><td><textarea cols="55" rows="4" name="prashanje">    </textarea></td></tr>
                   <tr><td></td><td >' . recaptcha_get_html($publickey) . '<button name="btn" value="submit">Enter</button></td></tr>
                   </form></table>
                 </div>';

And this is the validation code:

if (isset($_POST['btn'])) {
    $name = mysql_real_escape_string(strip_tags($_POST['name']));
    $prashanje = mysql_real_escape_string(strip_tags($_POST['prashanje']));
    $date = time();

    # 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) {
            $query = "INSERT INTO prashanja VALUES(NULL,'$name','$prashanje','','$date')";
            if (!mysql_query($query))
                echo 's... happens';
            else
                echo 'cool';
        } else {
            # set the error code so that we can display it
            $error = $resp->error;
        }
    }
}

These segments are on the same page. The first segment generates the form and the second segment checks if the correct button is pressed, initializes the variables and than checks the recaptcha and does some operations. I get the error "Undefined index: recaptcha_response_field". I suspect that recaptcha won't work if the validation is done on the same page as the form that sent the variables. Are my suspicions correct and is it better if I move the validation code to another file?

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
Martin
  • 710
  • 2
  • 11
  • 18
  • 1
    possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Sumurai8 Jan 03 '14 at 13:33
  • Is there any field in your form with this name "recaptcha_response_field" ? I did not see any field with this name in your html form. If this field is not in the form then this error will be for this reason – Awlad Liton Jan 03 '14 at 13:36
  • @AwladLiton doesn't recaptcha create that field with recaptcha_get_html($publickey) ? – Martin Jan 03 '14 at 13:37
  • I think You should have add generated html also. May be there is no input field is submitting with this name? – Awlad Liton Jan 03 '14 at 13:39
  • 1
    First var_dump $_POST, you can see what is in there and if the `recaptcha_response_field` is really there – Ronald Swets Jan 03 '14 at 13:42
  • Try to inspect your form element and check that what are the fields generated or var_dump post – Awlad Liton Jan 03 '14 at 13:43
  • @RonaldSwets as I said earlier, recaptcha_response_field is not sent but all the other fields in the form are. I double checked the hierarchy in the html and everything seems correct. – Martin Jan 03 '14 at 13:46
  • So recaptcha_response_field were not in post data. You are missing something in recaptcha library. – Awlad Liton Jan 03 '14 at 13:49

2 Answers2

3

It works if you validate on the same page. You are accessing the variable for checking:

if ($_POST["recaptcha_response_field"]) {

That most likely causes the error, it should be

if (isset($_POST["recaptcha_response_field"])) {
Lorenz
  • 2,179
  • 3
  • 19
  • 18
  • That should eliminate the notice but not the problem, the response field will still be empty and the functions inside the if will never be executed. – Martin Jan 03 '14 at 13:35
1

Move the form tag outside of the table tag. I don't know why but it worked.

Martin
  • 710
  • 2
  • 11
  • 18