Search code examples
phparrayspostvar-dump

Why Am I Getting an Undefined Index Error When var_dump($_POST) Shows an Array?


I have a form that is posting data to a PHP page on the same local site. I know that data is posting correctly because var_dump($_POST) outputs an array with the expected information, as shown below.

However, when I try to set a variable to one of the values in the array, I get an error notice in the logs saying that I have an undefined index (for the email and g-captcha response field) and the PHP code does not work as expected.

I have been banging my head to try and figure out why this is happening for quite some time and other similar questions haven't revealed an answer. Any help would be appreciated. Thanks!

Form code:

<form action="recaptcha.php" method="post" class="pardot-email-form-handler" id="carter" novalidate>
    <input class="one-field-pardot-form-handler" maxlength="80" name="email" id="email" size="20" type="email" placeholder="Enter Email Address" required="required" />
    <div style="position:absolute; left:-9999px; top: -9999px;">
        <label for="pardot_extra_field">
            Comments
        </label>
        <input type="text" id="pardot_extra_field" name="pardot_extra_field">
    </div>
    <button class="g-recaptcha" data-sitekey="6LeErDUUAAAAAEOYqDrylFnEwQkYp7qmPjs2_Z0o" data-callback="captchaSubmit" data-badge="inline">
        Submit
    </button>
</form>

PHP Code:

var_dump($_POST);
// reCaptcha info
$secret = "anonymous";
$remoteip = $_SERVER["REMOTE_ADDR"];
$url = "https://www.google.com/recaptcha/api/siteverify";

// Form info
$email = $_POST["email"];
$response = $_POST["g-recaptcha-response"];

// Curl Request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
    'secret' => $secret,
    'response' => $response,
    'remoteip' => $remoteip
    ));
$curlData = curl_exec($curl);
curl_close($curl);

// Parse data
$recaptcha = json_decode($curlData, true);
if ($recaptcha["success"])
    echo "Success!";
else
    echo "Failure!";

Var_Dump results:

  array (size=3)
  'email' => string 'an email address' (length=21)
  'pardot_extra_field' => string 'a comment' (length=12)
  'g-recaptcha-response' => string 'a long response' (length=441)

PHP error message:

[01-Jan-2018 15:42:02 UTC] PHP Notice: Undefined index: first in C:\wamp\www\local\public\recaptcha.php on line 17

[01-Jan-2018 15:42:02 UTC] PHP Stack trace:

[01-Jan-2018 15:42:02 UTC] PHP 1. {main}() C:\wamp\www\local\public\recaptcha.php:0

[01-Jan-2018 15:42:02 UTC] PHP Notice: Undefined index: last in C:\wamp\www\local\public\recaptcha.php on line 18

[01-Jan-2018 15:42:02 UTC] PHP Stack trace:

[01-Jan-2018 15:42:02 UTC] PHP 1. {main}() C:\wamp\www\local\public\recaptcha.php:0


Solution

  • From the error message that you have added, the code is looking for the index first/last in an array and unable to find it.

    But there does not seem to be any such reference of these key names in your code.

    There does not seem to be an array with indexes first or last in your code.

    And also there are no form input tags named first or last.

    Check if you are referring the proper file.