0

I have a problem with my PHP file. I want to save data from my HTML form to external JSON file "data.json". When I fire button to save it it returns message that everything is fine and data is added. Unfortunetaly, when I check my data.json file, it contains null instead of data from a form. Here is my code:

HTML:

 <form class="form form-group" action="save.php" method="POST">
   <label class="control-label" for="focusedInput">Nazwa knajpy</label>
   <input name="name" class="input form-control" id="focusedInput" type="text" value="Wpisz nazwę knajpy">

   <label class="control-label" for="focusedInput">Kategoria knajpy</label>
   <input name="category" class="input form-control" id="focusedInput" type="text" value="Bar/restauracja">

   <label class="control-label" for="focusedInput">Adres</label>
   <input name="addres" class="input form-control" id="focusedInput" type="text" value="Wpisz adres knajpy">

   <label class="control-label" for="focusedInput">Nazwa zdjęcia</label>
   <input name="img" class="input form-control" id="focusedInput" type="text" value="Wpisz nazwę zdjęcia jpg">

   <label class="control-label" for="focusedInput">Cena</label>
   <input name="price" class="input form-control" id="focusedInput" type="text" value="Wpisz cenę piwa">

   <input type="submit" class="btn btn-primary" value="Dodaj knajpę">
  </form>

save.php:

<?php

   $myFile = "data/data.json";
   $arr_data = array(); // create empty array

  try
  {
       //Get form data
       $formdata = array(
          'name'=> $_POST['name'],
          'category'=> $_POST['category'],
          'address'=>$_POST['address'],
          'img'=> $_POST['img'],
          'price'=> $_POST['price']
       );

       //Get data from existing json file
       $jsondata = file_get_contents($myFile);

       // converts json data into array
       $arr_data = json_decode($jsondata, true);

       // Push user data to array
       array_push($arr_data,$formdata);

       //Convert updated array to JSON
       $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";
   }

?>

Thanks for help!

1
  • Start debugging - var_dump all your variables. Commented Apr 2, 2017 at 15:42

1 Answer 1

3

This line is probably causing issues: $arr_data = json_decode($jsondata, true);

You should make sure it's not empty before you start working with it.

You could add a check like: $arr_data = !empty($jsondata) ? json_decode($jsondata, true) : array();

You may want to perform another check after that as well to make sure you have an array as you expect.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.