Change JSON file on server

Asked

Viewed 1,323 times

2

I have the following form:

 <form method="POST">
   Title <input type="text" onchange="Function()">
   X <input type="text" onchange="Function()">
   Y <input type="text" onchange="Function()">

  /* others fields */

</form>

and the test.json file

{
    "test":[    
        {
            "title" : "",
            "x" : "",
            "y" : ""        
        }   
    ]
}

How do I get an action onchange inputs and the test.json file be changed and saved automatically to the server?

I’m using PHP, so I’ll put the contents of the JSON file in an array:

$jsondados = file_get_contents("test.json");
$json = json_decode($jsondados,true);

Then I can access the array and put in the variables I want.

I would like suggestions/indications of functions or how to get what is typed in the form to be automatically passed to the php variables.

  • every time you press a key you will make a submite to the server, that’s it?

  • this, but updating the test.json file, or if there is no need for the test.json file another way to update the variable in php according to what is typed in the form

  • JSON is used as a transport, usually you write the data into a database. Write the JSON itself, only if it is for study or for some very simple application, with little data.

  • are only 3 fields is very simple

  • I made a simple application that generates image using the GD library, and the text variable, paddind x and y are the ones that must be filled according to the form

1 answer

1

Before reading the file you have to receive the form data in a array of PHP.

$dados_do_formulario = array();

$dados_do_formulario = filter_input(INPUT_POST, 'nome_do_formulario'); /*eu uso esse modo que é igual ao $_POST mesmo, porque dá pra filtrar a variável depois se quiser*/

Second of all, you have to open the JSON file and then decode it as you are doing already.

/* pego o arquivo JSON */

$arquivo_json = file_get_contents("test.json");

/* "transformo" temporariamente ele num array() que o PHP entenda*/
$decodifica_json = json_decode($arquivo_json);

Then you can use array_push() from PHP to include the form data in the JSON array (I’m using a simple inclusion as an example, but before including, you can take an existing index and change it or create a function to access the index and delete it, for example).

/* agora, usando o array_push() do PHP, você pode incluir dentro do arquivo JSON (que agora é o array() $decodifica_json) os dados do POST*/

array_push($decodifica_json, $dados_do_formulario);

Then you have to encode the JSON again, when everything is done and then save the file, using file_put_content() or a sequence with fwrite() and fclose().

/* aqui codifico o array() do PHP em um formato JSON novamente */
$arquivo_json_alterado = json_encode($decodifica_json);

/* aqui eu salvo o arquivo laterado JSON*/
file_put_contents('test.json', $arquivo_json_alterado);

  • In this case there is no need to encode (json_encode) an array in php for JSON. The Process I’m doing is reverse, in this sequence: Form HTML - > (javascript?) updates file Json -> php decodes Json -> takes the information in the php array and puts in the respective variables $title, $x and $y

  • If you want the saved file, after changing it, you will need to encode it again, otherwise PHP will save a Array&#xA;(&#xA; [0] => 1&#xA; [1] => 2&#xA; [2] => 3&#xA; [3] => 4&#xA; [4] => 5&#xA;)

  • then it is impossible even, on the client side using javascript open and save the test.json file on the server?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.