0

What I'm trying to do is to create a json file with a form, and then with a second form, update the json file that was created. All of this currently takes place in process.php. I'm having success with the second form if I name the json file as data.json first in process.php (now commented out), but I'm wanting to be able to create that name instead from an input from the first form. Can anyone see what I'm doing wrong? Nothing happens upon submitting the first form. No json file is created.

First form:

<form class="ui equal width form" action="server/php/process.php" method="POST" target="frame">
  <input type="text" name="filename" id="filename">
  <input type="submit" value="Save and continue" id="continue1">
</form>

Second form:

<form class="ui form" action="server/php/process.php" method="POST" target="frame" id="attribute-form">
  <input type="text" value="" class="ID" name="ID">
  <input type="hidden" value="" class="value-x" name="valueX">
  <input type="submit" value="Save" id="save-snippet">
</form>

process.php

    <?php

      // Form for saving template file name
      $myFile = $_POST['filename'];

      if(isset($_POST['filename'])){
        $handle = fopen("server/php/data/$myFile.json", "w+");
        fwrite($handle);
        fclose($handle);
      } else {
        echo 'Template has not been named. Please enter a name before saving.';
      }

      // Form for saving attribute JSON data
      //$myFile = "data/data.json";

      $arr_data = array(); // create empty array

      try
      {
           //Get form data
           $formdata = array(
              'ID'=> $_POST['ID'],
              'valueX'=> $_POST['valueX'],
           );
           //Get data from existing json file
           $jsondata = file_get_contents($myFile);

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

           $updateKey = null;
           foreach ($arr_data as $k => $v) {
              if ($v['ID'] == $formdata['ID']) {
                $updateKey = $k;
              }
           }
           if ($updateKey === null) {
              array_push($arr_data,$formdata);
           } else {
              $arr_data[$updateKey] = $formdata;
           }

           //Convert updated array to JSON
           $jsondata = json_encode($arr_data);

           //write json data into json file
           if(file_put_contents($myFile, $jsondata)) {
              echo 'Data successfully saved';
           }
           else
              echo "error";
      }
      catch (Exception $e) {
          echo 'Caught exception: ',  $e->getMessage(), "\n";
      }
?>
Todd Williams
  • 267
  • 2
  • 16
  • 1
    what about `$content` value? it is not defined in the code. May be the file is not being created due to undefined variable. – Cruzer Dec 22 '17 at 18:36
  • I got rid of that variable, thanks. Still the same result, though. – Todd Williams Dec 22 '17 at 18:42
  • 1
    Then check the directory URL and use this `$fp = fopen('server/php/data/'.$myfile.'.json', 'w'); fwrite($fp, json_encode($response)); fclose($fp);` ignore the `$response`. – Cruzer Dec 22 '17 at 18:47
  • It seems that you only write the `$myFile` file if `$_POST['filename']` is *not* set: `$if(!isset($_POST['filename']))`. Also, the dollar sign before the `if` might not belong. – showdev Dec 22 '17 at 18:48
  • are there any errors in the web server error logs? What is $if? – C Miller Dec 22 '17 at 18:48
  • @Cruzer, this code does work: `$myFile = $_POST['filename']; $fp = fopen('server/php/data/'.$myfile.'.json', 'w'); fwrite($fp, json_encode($response)); fclose($fp);` but it writes the file to the `server/php` folder instead of the `server/php/data` folder. – Todd Williams Dec 22 '17 at 19:16
  • @Cruzer It also doesn't give it a `.json` extension. It's a 4-bit file with the word `null` inside. – Todd Williams Dec 22 '17 at 19:24
  • You are only writing the file when the filename doesn't exist. `if(!isset($_POST['filename']))` – showdev Dec 22 '17 at 19:39
  • You've edited your question to fix a couple of issues. How has this changed the outcome? Are you still having the same problem? – showdev Dec 22 '17 at 20:46
  • I'm able to write the file, but `fopen('server/php/data/' . $myfile . '.json', 'w+');` still doesn't write a file with `.json` extension. My site is on GoDaddy, and I haven't been able to successfully view an error log. – Todd Williams Dec 22 '17 at 21:08
  • I've also decided to use 2 different PHP files, `add_template_file.php` to add the new empty array file, and then `process.php` to add/edit array data from the other form. `$myFile = $_POST['filename'] . ".json";` worked to add the extension. – Todd Williams Dec 22 '17 at 21:12
  • Sorry, I'm not sure because it seems your code/problem has evolved. But it will cause problems if you mix up `$myfile` and `$myFile`. It seems like getting to those error logs will be really helpful. Also see: [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display/21429652) – showdev Dec 22 '17 at 22:05

2 Answers2

0

First, your PHP script have a typo on the 5th line. There is no $ before the keyword if(Maybe that the reason why you can not run your code and get nothing on the first form-submit). The key point is the variable $myFile in your update-block-code $jsondata = file_get_contents($myFile);, the value(file path) is not correct. It's "/files/$myFile.json". (Oh..., you modified the PHP code, so the file path should be `"server/php/data/$myFile.json")

Zgoo
  • 40
  • 2
  • Thanks, Zgoo. The file writes now but it drops it in the `server/php` directory even though I have it set to `server/php/data/$myFile.json`. Also, there is not a `.json` extension on the file. It's just an empty file with the word `null` inside. Ideas? – Todd Williams Dec 22 '17 at 19:28
  • I notice it's dumping the file wherever I set the action path in the form. If I put a copy of `process.php` in `server/php/data/` and reference it there instead, then the file writes to that directory instead. Any way to correct this? – Todd Williams Dec 22 '17 at 19:35
  • (*^__^*) , Open the error reporting like `error_reporting(E_ALL)`, and then check the file info you just update on the first form use `var_dump($_FILES)`, and then check the permission of your PHP service user(got the `w+`?). – Zgoo Dec 22 '17 at 19:38
0

You need to check if $_POST['filename'] is set before getting the value inside it.

$if(isset($_POST['filename'])){
    $myFile = $_POST['filename'];
    //the rest of your code here
}
Liora Haydont
  • 1,252
  • 1
  • 12
  • 25