Write And Append Data In JSON File Using PHP

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

Write And Append Data In JSON File Using PHP

[{"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"},
{"name":"Tara","gender":"Female","age":"24","education":"BE","designation":"software","DOB":"1997-07-21"}]

The above output shows whatever data added in the form is appended in the JSON file.


Similar Articles