Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(PHP) Add incorrect text to json file

Tags:

arrays

php

My php script add incorrect value and overwrite the text of my json.json so I want don't overwrite it and add in the text with this format:

{
    name:"Google",
    url: "google.es",
},

and the script add the text as: ["{'name':'gmail', 'url':'gmail.com'}"] The objective is don't overwrite, just add content to the existing code.

html:

<!DOCTYPE html>
<html>

<head>
    <title>SSL Checker</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <script type="text/javascript" src="js/script.js"></script>
    <script type="text/javascript" src="js/json.json" charset="utf-8"></script>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $jsonContents = file_get_contents('js/json.json');
    $name = $_POST['addname'];
    $url = $_POST['addlink'];
    $data = json_decode($jsonContents, true);
    $data[] = array("{'name':'$name', 'url':'$url'}");
    $json = json_encode($data);
    file_put_contents('js/json.json', $json);
}
?>
</head>
<body onLoad="start()">
    <div id="title">
        <h1>SSL Checker</h1>
    </div>
    <div id="data">
        <form method="POST" onsubmit="SSL.Add()">
            <input type="text" name="addname" id="add-name" placeholder="Name"></input>
            <input type="text" name="addlink" id="add-link" placeholder="Link"></input>
            <input type="submit" value="Add">
        </form>
        <div id="edit" role="aria-hidden">
            <form action="javascript:void(0);" method="POST" id="saveEdit">
                <input type="text" id="edit-name">
                <input type="submit" value="Edit" /> <a onclick="CloseInput()" aria-label="Close">&#10006;</a>
            </form>
        </div>
        <p id="counter"></p>
    </div>
    <div id="table">
        <table style="overflow-x:auto;">
            <tr>
                <th>Sites:</th>
            </tr>
            <tbody id="urls">
            </tbody>
        </table>
    </div>
</body>
</html>

JSON file:

var Checker = [{
        name:"Google",
        url: "google.es",
    },
    {
        name:"Yahoo",
        url: "yahoo.com",
    }
]

js:

function start() {
    var SSL = new function() {
        //List urls to check
        this.el = document.getElementById('urls');
        this.Count = function(data) {
            var el = document.getElementById('counter');
            var name = 'url';

            if (data) {
                if (data > 1) {
                    name = 'urls';
                }
                el.innerHTML = 'There are:' + ' ' + data + ' ' + name;
            } else {
                el.innerHTML = 'No ' + name;
            }
        };
        //Box/Table Configuration (Sites/edit/delete)
        this.FetchAll = function() {
            var data = '';

            if (Checker.length > 0) {
                for (i = 0; i < Checker.length; i++) {
                    data += '<tr>';
                    data += '<td><a href="http://' + Checker[i].url + '">' + Checker[i].name + '</a></td>';
                    data += '<td><button onclick="SSL.Edit(' + i + ')">Edit</button></td>';
                    data += '<td><button onclick="SSL.Delete(' + i + ')">Delete</button></td>';
                    data += '</tr>';

                }
            }

            this.Count(Checker.length);
            return this.el.innerHTML = data;
        };
        //Add name
        this.Add = function() {
            el = document.getElementById('add-name');
            el1 = document.getElementById('add-link')
            var url = el.value;
            var url1 = el1.value;
            if (url) {
                if (url) Checker.push({
                    "name": url,
                    "url": url1
                })
                el.value = '';
                this.FetchAll();
            }
        }

        //Edit
        this.Edit = function(item) {
            var el = document.getElementById('edit-name');
            var el1 = document.getElementById('edit-name1');
            el.value = Checker[item].name;
            el1.value = Checker[item].url;
            document.getElementById('edit').style.display = 'block';
            self = this;
            document.getElementById('saveEdit').onsubmit = function() {
                var url = el.value;
                var url1 = el1.value;
                if (url) {
                    Checker[item].url = url1.trim();
                    Checker[item].name = url.trim();
                    self.FetchAll();
                    CloseInput();
                }
            }
        };
        //Delete
        this.Delete = function(item) {
            Checker.splice(item, 1);
            this.FetchAll();
        };

    };

    SSL.FetchAll();
    //Close button (Edit bar)
    function CloseInput() {
        document.getElementById('edit').style.display = 'none';
    }
    window.CloseInput = CloseInput;
    window.SSL = SSL;
}
like image 453
Joanmi Avatar asked Mar 09 '26 02:03

Joanmi


1 Answers

You need to load old JSON first and push in that array

$json = json_parse(file_get_contents('js/json.json'));
if(!$json){ $json = []; };
$json[] = ['name' => $name, 'url' => $url];
$json = json_encode($json);
file_put_contents('js/json.json', $json);
like image 105
Mihai Iorga Avatar answered Mar 10 '26 16:03

Mihai Iorga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!