您好我尝试使用PHP更新JSON文件,但它没有像我预期的那样工作,您能尝试纠正它吗?我想改变" gfdhdfghdfh" to" google.es" 我添加了index.php和所有代码,因为脚本" update.php"不发布。 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);
?>
它取自html代码的2个框的值。 (姓名和网址)
JSON文件:
[{"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">✖</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>
答案 0 :(得分:1)
你的if语句中有一个双等号。将其更改为等于分配值。
$data[$key]['url'] == $url;
到
$data[$key]['url'] = $url;
更新
根据以下粘贴和注释,它是不发送的表单POST,因为表单元素只有id而不是名称。