Write And Append Data In JSON File Using PHP - C# Corner
Maybe your like
In this article, I will explain how to write and append a data in JSON file while submitting a form. Here using a few PHP functions we are going to add a data in JSON format. I have one simple HTML form with some input boxes like name, gender, age, etc.
Using PHP scripting we can store a form value in array format. After that will convert the array into JSON data using json_encode() predefined function. Then at last we can move the data to JSON format file.
In below snippet is my HTML form given
<div class="container" style="width:500px;"> <h4 align="">User Details</h4><br /> <form method="post"> <label>Name</label> <input type="text" name="name" class="form-control" /><br /> <label>Gender</label> <input type="radio" name="gender" value="Male" />Male <input type="radio" name="gender" value="Female" /> Female<br /> <label>Age</label> <input type="number" name="age" class="form-control" /><br /> <label>Education</label> <input type="text" name="education" class="form-control" /><br /> <label>Designation</label> <input type="text" name="designation" class="form-control" /><br /> <label>DOB</label> <input type="date" name="dob" class="form-control" /><br /> <input type="submit" name="submit" value="submit" class="btn btn-info" /><br /> </form> </div>While submitting a form we can validate the POST param input values using PHP script
if(empty($_POST["name"])) { $error = "<label class='text-danger'>Enter Name</label>"; } else if(empty($_POST["gender"])) { $error = "<label class='text-danger'>Enter Gender</label>"; } else if(empty($_POST["education"])) { $error = "<label class='text-danger'>Enter education</label>"; } else if(empty($_POST["designation"])) { $error = "<label class='text-danger'>Enter Designation</label>"; } else if(empty($_POST["age"])) { $error = "<label class='text-danger'>Enter age</label>"; }Once all data get validated, we need to add in JSON file. So using array function all data is stored in array format and then it converts into JSON data with the use of json_encode() function.
If the file already exists, we can append it with that old data. file_get_contents() function will get the current data in a file then we can add new data. In case we are not having a file will create a new one and write the data in the same way.
if(file_exists('file.json')) { $final_data=fileWriteAppend(); if(file_put_contents('file.json', $final_data)) { $message = "<label class='text-success'>Data added Success fully</p>"; } } else { $final_data=fileCreateWrite(); if(file_put_contents('file.json', $final_data)) { $message = "<label class='text-success'>File createed and data added Success fully</p>"; } } function fileWriteAppend(){ $current_data = file_get_contents('file.json'); $array_data = json_decode($current_data, true); $extra = array( 'name' => $_POST['name'], 'gender' => $_POST["gender"], 'age' => $_POST["age"], 'education' => $_POST["education"], 'designation' => $_POST["designation"], 'DOB' => $_POST["dob"] ); $array_data[] = $extra; $final_data = json_encode($array_data); return $final_data; } function fileCreateWrite(){ $file=fopen("file.json","w"); $array_data=array(); $extra = array( 'name' => $_POST['name'], 'gender' => $_POST["gender"], 'age' => $_POST["age"], 'education' => $_POST["education"], 'designation' => $_POST["designation"], 'dob' => $_POST["dob"] ); $array_data[] = $extra; $final_data = json_encode($array_data); fclose($file); return $final_data; }The final output of the code is given below.
[{"name":"sona","gender":"female","age":"34","education":"dsaas","designation":"FSDF","dob":"2021-12-13"}, {"name":"sonakchi","gender":"Female","age":"27","education":"MBBS","designation":"Doctor","DOB":"1996-09-24"}This user details added in form while on submitting data will append to JSON file
The above output shows whatever data added in the form is appended in the JSON file.
Tag » Add Element Json Php
-
How To Add Element To JSON Object Using PHP? - Stack Overflow
-
Php Append Data Into Existing Json Code Example
-
PHP Add JSON Object Data Example - Pakainfo
-
JSON PHP - W3Schools
-
PHP And JSON - W3Schools
-
How To Add Element To Json Object Using Php | Learn
-
Append Data To JSON File Using PHP - YouTube
-
Json_decode - Manual - PHP
-
Json_encode - Manual - PHP
-
How To Create An Array For JSON Using PHP? - GeeksforGeeks
-
How To Insert A New Key Value To The Existing JSON- PHP
-
Adding An Array To JSON Object Inside An Array - Laracasts
-
Dealing With JSON Arrays And Objects In PHP - Elastic
-
How To Add Element To JSON Object Using PHP? - Array Overflow
-
Eloquent: Serialization - The PHP Framework For Web Artisans
-
Using PHP Arrays With JSON - IBM
-
Add Element To A JSON File??
-
How To Encode And Decode JSON Data In PHP - Tutorial Republic
-
PHP Array To JSON: How To Use PHP Json_encode() - AppDividend