0

Hello I'm trying to update a JSON file using PHP but it isn't working as I expect, can you try to correct it? I want change "gfdhdfghdfh" to "google.es" I added the index.php with all code because the script "update.php" is not posting. update.php:

<?php
var_dump($_POST);
$jsonContents = file_get_contents('js/json.json');
$name = $_POST['edit-name'];
$url = $_POST['edit-name1'];
$data = json_decode($jsonContents, true);
foreach ($data as $key => $value) {
    if ($value['name'] == $name) {
        $data[$key]['url'] == $url;
    }
};
$json = json_encode($data);
file_put_contents('js/json.json', $json);

?>

It takes the values from 2 boxes of the html code. (Name and Url)

JSON FILE:

[{"name":"gfdhdfghdfg","url":"gfdhdfghdfh"}]

index.php:

<?php
//ADD Links
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    var_dump($_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);
    header('Location: http://URL/index.php');
}
?>
<!DOCTYPE html>
<html>

<head>
    <title>SSL Checker</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link rel="icon" type="image/png" href="security-ssl.png"/>
    <script type="text/javascript" charset="utf-8">
        var Checker = <?php echo file_get_contents('js/json.json'); ?>;
    </script>
    <script type="text/javascript" src="js/script.js"></script>

</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 type="text" name="addlink" id="add-link" placeholder="Link" />
            <input type="submit" value="Add" />
        </form>
        <div id="edit" role="aria-hidden">
            <form action="update.php" method="POST" id="saveEdit">
                <input type="text" id="edit-name">
                <input type="text" id="edit-name1">
                <input type="submit" value="Edit" /> <a onclick="CloseInput()" aria-label="Close">&#10006;</a>
            </form>
        </div>
        <input type="text" id="dl">
        <p id="counter"></p>
    </div>
    <div id="table">

       <table style="overflow-x:auto;">
       <tr>
           <th>Sites:</th>
       </tr>
           <tbody id="urls">
        </table>

    </div>
</body>
</html>
Anthy
  • 60
  • 1
  • 8

1 Answers1

1

You have a double equals in your if statement. Change it to one equals to assign a value.

$data[$key]['url'] == $url;

to

$data[$key]['url'] = $url;

UPDATE

Based on following pastes and comments, it's the form POST which isn't sending, because the form elements only have an id and not a name.

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39