-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",
    }
]
Joanmi
  • 442
  • 3
  • 19
  • 3
    There does not seem to be any PHP here? You seem to be looking for some javascript?? – Paul Coldrey May 22 '18 at 11:45
  • Possible duplicate of [Add new data into PHP JSON string](https://stackoverflow.com/questions/1745052/add-new-data-into-php-json-string) – bueltge May 22 '18 at 11:45
  • No it isn't I'm asking for add keys for example: { "name":"google", "url":"google.es" } – Joanmi May 22 '18 at 11:47
  • I never programmed PHP so, I'm looking what to do @PaulColdrey – Joanmi May 22 '18 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. – deceze May 22 '18 at 11:54
  • Can't I do it as my js array? – Joanmi May 22 '18 at 12:05
  • "Can't" is a strong word, but it certainly is *needlessly complicated and probably error prone.* – deceze May 22 '18 at 12:11

1 Answers1

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?

stormwild
  • 2,855
  • 2
  • 31
  • 38
  • 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 – Joanmi May 22 '18 at 13:41
  • And the values have to be introduced by the user – Joanmi May 22 '18 at 13:44