3
I’m trying to save a file dados.json
, where the form data is sent via POST to a file dados.php
by AJAX.
The code of dados.php
is as follows:
$array = array();
$fp = fopen("dados.json", "a");
$escreve = fwrite($fp, $array[0] = json_encode($_POST));
fclose($fp);
The structure of the data that the dados.php
generates for the dados.json
is:
{"nome":"fulano","numero":"1"}
The problem is that I wanted to make a structure in which these data sent were inside brackets and that at the end of each key had a comma, but every time I send the data, they are saved in the dados.json
is generated like:
{"nome":"fulano","numero":"1"}{"nome":"fulano2","numero":"3"}
It is not separated by comma. I tried otherwise with the following code:
$array = array($_POST);
$jsonDados = json_encode($array);
$fp = fopen("dados.json", "a");
$escreve = fwrite($fp, $jsonDados);
fclose($fp);
But now the dados.json
was like this:
[{"nome":"fulano","numero":"1"}][{"nome":"fulano2","numero":"3"}]
I just want to make this data saved like this:
[{"nome":"fulano","numero":"1"},{"nome":"fulano2","numero":"3"}]
And that every time there is a submission form to the dados.php
, it saves JSON by separating keys by commas inside the brackets.
ok, but the result on the.json data will always be
{"nome":"fulano","numero":"1"}
and every time the data is sent from the form will be saved in sequence ex{"nome":"fulano","numero":"1"}{"nome":"fulano","numero":"1"}
i wanted a structure in which every time there is a submission of the form to the.php data, it saves the json by separating the keys by commas inside the brackets, as I mentioned in the question, there is a way to do this?– Jonny Max
I changed the answer
– Roberto de Campos