Insert Data To JSON File Using PHP | CampCodes

Insert Data To JSON File using PHP

July 10, 2020
insert data to json file

How to Insert Data To JSON File using PHP

In this tutorial, we will create an Insert Data To JSON File using PHP. This code can insert data to the JSON file when the user submits the form input. The system uses the PHP POST method to call a function that adds the form data as a JSON object to a JSON file using file_put_contents(). This is a user-friendly kind of program feel free to modify it.

We will be using JSON as data management to store user data, and it will act as database storage. It is an open-standard file format that uses human-readable text to transmit data objects into the local server.

insert data to json file

insert data to json file

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server

And, this is the link for the bootstrap that I used for the layout design

Creating The Interface

This is where we will create a simple form for our application. To create a copy of the way and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="">Sourcecodester</a>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Insert Data To JSON File</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-4">
            <form method="POST" action="insert.php">
                <div class="form-group">
                    <input type="text" class="form-control" name="firstname" required="required"/>
                <div class="form-group">
                    <input type="text" class="form-control" name="lastname" required="required"/>
                <div class="form-group">
                    <input type="text" class="form-control" name="address" required="required"/>
                <center><button class="btn btn-primary" name="insert">Insert</button></center>
        <div class="col-md-8">
            <table class="table table-bordered table-striped">
                <thead class="alert-info">
                        $data = file_get_contents('members.json');
                        $data = json_decode($data);
                        foreach($data as $fetch){
                            <td><?php echo $fetch->firstname?></td>
                            <td><?php echo $fetch->lastname?></td>
                            <td><?php echo $fetch->address?></td>

Creating the Main Function

This code contains the main function of the application. This code will insert a data to JSON file when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below.

        "firstname": "John",
        "lastname": "Smith",
        "address": "New York"
        "firstname": "Claire",
        "lastname": "Temple",
        "address": "Racoon City"


        $data = file_get_contents('members.json');
        $json = json_decode($data);
        $array = array(
            'firstname' => $_POST['firstname'],
            'lastname' => $_POST['lastname'],
            'address' => $_POST['address']
        $json[] = $array;
        $json = json_encode($json, JSON_PRETTY_PRINT);
        file_put_contents('members.json', $json);

There you have it we successfully created on How to add Data To JSON File using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


PHP - Insert Data To JSON File Source Code 337 KB 1544 downloads

Please go back to the description page. Just Click the Red Download Button on the...

Leave a Reply

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