0
if (isset($_POST['btnSubmit']))
    {
        if(isset($_POST['g-recaptcha-response'])){
          $captcha=$_POST['g-recaptcha-response'];

        }
        echo $captcha;
        if(!$captcha){

          echo '<h2>Please check the the captcha form.</h2>';
          exit;
        }
        $secretKey = "================";
        $ip = $_SERVER['REMOTE_ADDR'];
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
        $responseKeys = json_decode($response,true);
        if(intval($responseKeys["success"]) !== 1) {
          echo '<h2>You are spammer ! Get the @$%K out</h2>';
        } else {
          echo '<h2>Thanks for posting comment.</h2>';
}

this is my code when click submit form then undefined variable $chaptcha please help me

Hydrothermal
  • 4,851
  • 7
  • 26
  • 45
Md Shoaib
  • 9
  • 1
  • 1
    Have you confirmed that `$_POST['g-recaptcha-response']` is defined? – IzzEps Jul 24 '16 at 04:37
  • 1
    Post your form code too – Panda Jul 24 '16 at 04:38
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Jocelyn Jul 24 '16 at 07:51

1 Answers1

0

PHP throws notices if you reference a variable that hasn't been created yet, although the code still "works", Read this answer

try something like this :

<?php

if (isset($_POST['btnSubmit'])) {
    if(isset($_POST['g-recaptcha-response'])){
      $captcha=$_POST['g-recaptcha-response'];
      echo $captcha;
    }else {
      echo '<h2>Please check the the captcha form.</h2>';
      exit;
    }

    $secretKey = "================";
    $ip = $_SERVER['REMOTE_ADDR'];
    $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
    $responseKeys = json_decode($response,true);

    if(intval($responseKeys["success"]) !== 1) {
      echo '<h2>You are spammer ! Get the @$%K out</h2>';
    } else {
      echo '<h2>Thanks for posting comment.</h2>';
    }
}

?>
Community
  • 1
  • 1
Amr SubZero
  • 1,196
  • 5
  • 19
  • 30