The purpose of this article is to append data to a JSON file through HTML form using PHP.
Approach 1: If the JSON file is not created then we create a new JSON file, send data to it, and append data in it. To see how to create a JSON file by taking data from the HTML form, refer this link.
Approach 2: If the JSON file is already created then we directly append data to the JSON file. To send data from HTML form to JSON file we are using json_encode() function which returns a JSON encoded string.
We are making an array of values that the user fills in the HTML form. Then we pass this array into json_encode() function. The json_encode() function returns a JSON encoded string. To create a JSON file we used PHP file_put_contents() which is used to write data to a file. We pass 2 arguments in file_put_contents() function. The first parameter is our file name in which we want to store data in the JSON format and the second is our PHP get_data() function.
According to the first approach, we will successfully create a JSON file using the json_encode() function. Now our JSON file is created. The next task is to append data to that JSON file. To append data to the JSON file we have to store the previous data to a variable. To get the data of our JSON file we will be using the file_get_contents() function. The file_get_contents() reads a file into a string. To decode the string, the json_decode() function is used which is an in-built function in PHP used to decode a JSON string. The function converts a JSON encoded string into a PHP variable. The HTML data in an array is assigned to our decoded string. The json_encode() function and file_put_contents() are used to encode string and put the contents to JSON file respectively.
Example: The following HTML and PHP code demonstrates the above approach.
PHP Code: The following is the “gfg.php” file used in the above HTML file.
Output: The content of the “StudentsData.json” file shows the data in JSON format.
- How to send data of HTML form directly to JSON file?
- How to fetch data from JSON file and display in HTML table using jQuery ?
- How to Convert Data URI to File then append to FormData?
- How to create a dynamic JSON file by fetching data from localserver database ?
- How to download a CSV file in PHP that is triggered through a URL ?
- How to read and write JSON file using Node.js ?
- Export JSON to CSV file in Angular
- How to update dependency in package.json file ?
- How to work with Node.js and JSON file ?
- How to Convert CSV to JSON file having Comma Separated values in Node.js ?
- JSON full form
- How to create an array for JSON using PHP?
- How to load data from JSON into a Bootstrap Table?
- PHP append one array to another
- ArrayObject append() function in PHP
- PHP | ArrayIterator append() Function
- PHP | AppendIterator append() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.