How to Append form data to JSON File in PHP Programming Language. I have create one HTML form, I want to add the form data into one json file. For this I have use file_get_contents() and file_put_contents(). File_get_contents() function get json file contents and with the help of php I have convert that content into array formate by using json_decode() function. After this now I want to add form data into this array. For this I have store form data into one array when user submit form, then at that time that form data will store into array. After this I append this form data array into array which I have converted from json file. Now Form data are added into array, so I convert that array into json formate by using json_encode() function. Now all data are converted into json formate, so last I have use file_put_contents() function. With the help of this function I have overwrite this json data on to file.
Source Code
append_json_data.php
<?php
$message = '';
$error = '';
if(isset($_POST["submit"]))
{
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["designation"]))
{
$error = "<label class='text-danger'>Enter Designation</label>";
}
else
{
if(file_exists('employee_data.json'))
{
$current_data = file_get_contents('employee_data.json');
$array_data = json_decode($current_data, true);
$extra = array(
'name' => $_POST['name'],
'gender' => $_POST["gender"],
'designation' => $_POST["designation"]
);
$array_data[] = $extra;
$final_data = json_encode($array_data);
if(file_put_contents('employee_data.json', $final_data))
{
$message = "<label class='text-success'>File Appended Success fully</p>";
}
}
else
{
$error = 'JSON File not exits';
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Webslesson Tutorial | Append Data to JSON File using PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container" style="width:500px;">
<h3 align="">Append Data to JSON File</h3><br />
<form method="post">
<?php
if(isset($error))
{
echo $error;
}
?>
<br />
<label>Name</label>
<input type="text" name="name" class="form-control" /><br />
<label>Gender</label>
<input type="text" name="gender" class="form-control" /><br />
<label>Designation</label>
<input type="text" name="designation" class="form-control" /><br />
<input type="submit" name="submit" value="Append" class="btn btn-info" /><br />
<?php
if(isset($message))
{
echo $message;
}
?>
</form>
</div>
<br />
</body>
</html>
Thank you!
ReplyDeleteThank you very much for the awesome tutorials. I wonder there a lesson/or if you can make one about how to edit(manipulate) a text (CSV) files using fgets. I need to read 2 text files, do some file manipulation and statistics calculation then output in html.
ReplyDeleteThanks
ReplyDeletethank you
ReplyDeleteGREAT
ReplyDelete{
ReplyDelete"user":[
{
"name": "ratul Ahmade",
"Gender": "male",
"designation": "Student"
},
{
"name": "ratul Ahmade",
"Gender": "male",
"designation": "Student"
},
]
}
I want like this layout . how can i do? I tried . But failed. I'm very new with json with php
Thank you
ReplyDelete