The task is to send data of HTML form directly to JSON file.
Approach: We have an HTML form containing several fields such as name, college, etc. We want to send the data of our HTML form directly to the JSON file. For this 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. Then json_encode() function returns a JSON encoded string. The whole task is implemented in a PHP function get_data(). To create a JSON file we used PHP function file_put_contents(). This function 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 second is our get_data() function.
Example: The following HTML and PHP codes demonstrates the above approach.
gfg.php This “gfg.php” file demonstrates the PHP code to which the HTML form contents are posted.
Output: The content of “gfg.json” file shows the following data in JSON format.
- How to append data in JSON file through HTML form using PHP ?
- How to fetch data from JSON file and display in HTML table using jQuery ?
- How to directly update a field by using ng-click in AngularJS ?
- How to create a dynamic JSON file by fetching data from localserver database ?
- Export JSON to CSV file in Angular
- How to read and write JSON file using Node.js ?
- 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
- PHP | Send Attachment With Email
- PHP | Spreadsheet_Excel_Writer | send() Function
- How to send HTTP response code in PHP?
- How to send a GET request from PHP?
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.