2

I've seen similar posts to this question but I can't seem to figure it out. I have a small PHP script that reads and writes form input to a JSON file, like this –

  $file = 'data.json';

  $arr_data = array();

  $formdata = array(
        'name' => strip_tags( trim($_POST['formName']) ),
        'email' => $email,
        'phone' => strip_tags( trim($_POST['formPhone']) ),
        'message' => strip_tags( trim($_POST['formMessage']) )
        // also tested this just using reg strings
  );

  $jsondata = file_get_contents($file);

  //var_dump($jsondata); returns whatever string content is in the file, so seems to work

  $arr_data = json_decode($jsondata, true);

  array_push($arr_data, $formdata);

  //var_dump($arr_data); returns NULL, not sure what happens here

  $jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);

  file_put_contents($file, $jsondata);

Any ideas? Using PHP 5.5.9, checked that files are writeable. Both files have UTF8 encoding.

6
  • provide Your data.json file Commented Sep 26, 2017 at 3:57
  • 1
    Try using var_dump($arr_data) immediately after $arr_data = json_decode($jsondata, true). Also, make sure you can see any errors. Place this at the top of your script ~ error_reporting(E_ALL); ini_set('display_errors', 'On'); Commented Sep 26, 2017 at 3:58
  • My data.json file is just blank. Should there be some pretext content in order for it to work? Commented Sep 26, 2017 at 3:59
  • 2
    yes, incase it's blank, make $arr_data = array() or preset the json file to [] Commented Sep 26, 2017 at 3:59
  • 1
    instead of array_push() use array_merge() function to append one array to another without comparing their keys. Commented Sep 26, 2017 at 4:01

2 Answers 2

1

json_decode() will return NULL if the input is blank. Try this to ensure your $arr_data is an array...

$arr_data = json_decode($jsondata, true);
if ($arr_data === null) {
    $arr_data = [];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wonderful! That did it :) And always good to learn something new.
1

maybe you want this code

<?php
  $file = 'data.json';
  $email='your mail info';
  $arr_data = array();

  $formdata = array(
        'name' => strip_tags( trim($_POST['formName']) ),
        'email' => $email,
        'phone' => strip_tags( trim($_POST['formPhone']) ),
        'message' => strip_tags( trim($_POST['formMessage']) )
        // also tested this just using reg strings
  );

  $jsondata = file_get_contents($file);

  //var_dump($jsondata); returns whatever string content is in the file, so seems to work

  $arr_data = json_decode($jsondata, true);

  // I added for if data.json is null to empty array
  $arr_data = is_null($arr_data)?array():$arr_data;

  array_push($arr_data, $formdata);

  //var_dump($arr_data); returns NULL, not sure what happens here

  $jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);

  file_put_contents($file, $jsondata);
?>

others

you should change your code to keep post data not null or default values and pay attention to file_get_contents method about file length

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.