Php error, array

I keep getting this error for all my input data and I don’t know how to fix it.
ex. Warning : Undefined array key “name”

This is just some info that I have (it’s a long data input):

?php

$name = "";

$address = "";

$phonenumber = "";

$age = "";

if (isset($_POST[‘SubmitButton’])) {
// gets file name

    $myFile = "data.json";

    // instantiate array

    $arr_data = array();

    try {

        // get form data

        $formdata = array(

            'name' => $_POST['name'],

            'address' => $_POST['address'],

            'phonenumber' => $_POST['phonenumber'],

            'age' => $_POST['age'], 
      );

// get data from existing json file to append to

        $jsondata = file_get_contents($myFile); // data.json

        // converts json data to array

        $arr_data = json_decode($jsondata, true);

        // push user data to array

        array_push($arr_data, $formdata);

        // convert updated array back to json to write it

        $jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);

        // write json data into data.json file

        if (file_put_contents($myFile, $jsondata)) {

            echo 'Data successfully saved';

        } else {

            echo 'error';

        }

    } catch (Exception $e) {

        echo 'Caught Exception: ', $e->getMessage(), '\n';
  }

}

?>

Your $_POST array does not have the "name" key in it. Check your form input name attributes. you can use var_dump($_POST); to see what is being passed to PHP, that may help you.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service