-1

I'm trying to add values to a file but its my first time with PHP and I don't find the way how can I do it?

The objective is add the values from the formulary (add-name and add-link ID) to the JSON File with the same structure and save it. (Don't temporally it have to save it on the file). I'm asking to instert a key for example: { "name":"google", "url":"google.es }

<!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>
</head>

<body onLoad="start()">
    <div id="title">
        <h1>SSL Checker</h1>
    </div>
    <div id="data">
        <form action="javascript:void(0);" method="POST" onsubmit="SSL.Add()">
            <input type="text" id="add-name" placeholder="Name"></input>
            <input type="text" 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><br>
                <input type="text" id="edit-name1">
            </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:

var Checker = [{
        name:"Google",
        url: "google.es",
    },
    {
        name:"Yahoo",
        url: "yahoo.com",
    }
]
7
  • 3
    There does not seem to be any PHP here? You seem to be looking for some javascript?? Commented May 22, 2018 at 11:45
  • Possible duplicate of Add new data into PHP JSON string Commented May 22, 2018 at 11:45
  • No it isn't I'm asking for add keys for example: { "name":"google", "url":"google.es" } Commented May 22, 2018 at 11:47
  • I never programmed PHP so, I'm looking what to do @PaulColdrey Commented May 22, 2018 at 11:48
  • 1
    That is not JSON, that is Javascript. It'll be hard to programmatically edit Javascript files. If you'd make that pure JSON, you can do it by 1) reading the JSON file, 2) json_decode it to an array, 3) add the data, 4) json_encode it, 5) write it back to the file. Commented May 22, 2018 at 11:54

1 Answer 1

0
<?php

$checkers = []; // assign an empty array to the variable $checkers;

// cast associative array to an object and add to the checker array
$checkers[] = (object)['name' => 'Google', 'url' => 'google.es'];

// create an object using the predefined stdClass 
$item = new stdClass();
$item->name = 'Yahoo';
$item->url = 'yahoo.com';

$checkers[] = $item;

// ideally create your own class representing the checker object

echo json_encode($checkers);

Outputs:

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

Please see this link for more info: In PHP, how can I add an object element to an array?

Sign up to request clarification or add additional context in comments.

2 Comments

The objective is Add the values when I click "Add" and save it on the file and how the command know's where is the document to extract the information "google" for example? and where to save it
And the values have to be introduced by the user

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.