Jonathan Bishop Jonathan Bishop - 1 month ago 11
JSON Question

PHP Read default values from file

I'm having an issue with the way my script works. What is supposed to happen is: when the form loads, it reads the last-used data from a .json file and populates the fields. It works, but when new data is submitted it populates the form with old data and not the new data.

The first part reads the JSON file and builds the form

<?php $old = json_decode((file_get_contents(dirname(__FILE__)."/json.json")), true); ?>
<!--Building the form-->
<table>
<form action="edit.php" method="POST">
<label for="time">Time:</label>
<input type="text" name="time" id="time" value="<?php echo ($old['time']); ?>"></input>
<br />
<label for="event">Event:</label>
<input type="text" name="event" id="event" value="<?php echo ($old['event']); ?>"></input>
<br />
<label for="event">Notes:</label>
<input type="textarea" name="notes" id="notes" value="<?php echo ($old['notes']); ?>"></input>
<br />
<input type="submit" name="task" value="Go" />
<input type="submit" name="task" value="Clear" />
</form>


This next section decides what to do if the clear button is pressed

<?php if ($_POST['task'] == 'Clear'){
$time = '';
$event = '';
$notes = '';
$remainingtime = '';
$task = 'clear';}
else {
?>


This final section populates the new array and saves it as the JSON file.

<?php if ($old['time'] != $_POST['time']){
$time = preg_replace("/[^0-9]/","",$_POST['time']);
$remainingtime = ((microtime())+($timein*60)*1000);
$task = 'countdown';}
else {
$remainingtime = $old['time'];
}

$event = $_POST['event'];
$notes = $_POST['notes'];
$remainingtime = '';
$task = 'display';
}

$new = array ('time' => $time, 'event' => $event,'notes' => $notes , 'remainingtime' => $remainingtime , 'task' => $task);


file_put_contents((dirname(__FILE__).'/json.json'), json_encode($new));
}

?>


When the clear button is pressed repeatedly, the form is cleared and the JSON file is cleared.
When the Go button is pressed the form and JSON file alternate between the new form submission and what is in the JSON file.

Any help would be appreciated.

NB Time comparison doesn't work in my current code

Answer

You say "this final section" does the saving. If you load the data at the top of the page and save it at the bottom, after you've generated the form, it will always be the old data that displays because that's what you loaded. You don't update the file until after you've loaded the old data and displayed it.

To fix this, change your code from this basic structure:

load data
display form using old data
decide what to do if the clear button is pressed
populate the new array
save the new array as the json file

to this:

decide what to do if the clear button is pressed
if there's new data:
    populate the new array
    save the new array as the JSON file
otherwise, load the old data
display form using the data (whether it's new or old

By the way, your comparison to $post['time'] doesn't make sense. The code you shared doesn't define $post (which is not the same as the superglobal $_POST, but is confusing).