How To Add Data To JSON File In PHP | CampCodes ...

How to Add Data to JSON File in PHP

June 11, 2020
add data to json file in php

Getting Started

I’ve used Bootstrap in this tutorial which is a CSS framework and is included in the downloadable of this tutorial, but if you want, you may download Bootstrap using this link.

Also, please take note that I’ll be using members.json that I have included in the downloadable of this tutorial.

Displaying a JSON Data

Next, we display the existing data in our JSON file and create the add/append form as well. Create a new record, name it as index.php and paste the codes below.

<!DOCTYPE html>
    <meta charset="utf-8">
    <title>How to Add/Append Data to JSON File using PHP</title>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
<div class="container">
    <h1 class="page-header text-center">Add/Append Data to JSON File</h1>
    <div class="row">
        <div class="col-sm-3 col-sm-offset-1">
            <form method="POST" action="add.php">
                <div class="form-group">
                    <input type="text" class="form-control" name="id">
                <div class="form-group">
                    <input type="text" class="form-control" name="firstname">
                <div class="form-group">
                    <input type="text" class="form-control" name="lastname">
                <div class="form-group">
                    <input type="text" class="form-control" name="address">
                <div class="form-group">
                    <input type="text" class="form-control" name="gender">
                <button type="submit" class="btn btn-primary" name="add">Add</button>
                    <div class="alert alert-info text-center" style="margin-top:20px;">
                        <?php echo $_SESSION['message']; ?>
        <div class="col-sm-7">
            <table class="table table-bordered table-striped">
                        $data = file_get_contents('members.json');
                        $data = json_decode($data);
                        foreach($data as $row){
                            echo "

Creating an Append Script

Lastly, we create a script that adds data to our JSON file. Please create a new record, name it as add.php and paste the codes below.

        $data = file_get_contents('members.json');
        $data_array = json_decode($data);
        //data in our POST
        $input = array(
            'id' => $_POST['id'],
            'firstname' => $_POST['firstname'],
            'lastname' => $_POST['lastname'],
            'address' => $_POST['address'],
            'gender' => $_POST['gender']
        //append the POST data
        $data_array[] = $input;
        //return to json and put contents to our file
        $data_array = json_encode($data_array, JSON_PRETTY_PRINT);
        file_put_contents('members.json', $data_array);
        $_SESSION['message'] = 'Data successfully appended';
        $_SESSION['message'] = 'Fill up add form first';

Leave a Reply

Your email address will not be published. Required fields are marked *