Answer the question
In order to leave comments, you need to log in
How to display data from json to the screen?
Form code that is processed on the main page
<?php
if(isset($_POST["name"]) && isset($_POST["number"])) {
$fp = fopen('data.json', 'a');
$arr = ['name' => $_POST['name'], 'number' => $_POST['number']];
$json_data = json_encode($arr).PHP_EOL;
fwrite($fp, $json_data);
fclose($fp);
}
?>
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Grid</title>
</head>
<body>
<table>
<tr>
<td>Name</td>
<td>Number</td>
</tr>
<?php
$r_json = file_get_contents("data.json");
$arr_json = json_decode("data.json", true);
?>
</table>
</body>
</html>
{"name":"1","number":"86"}
{"name":"12","number":"8"}
{"name":"13","number":"6"}
{"name":"dawd","number":"2134"}
{"name":"авф","number":"213"}
{"name":"14","number":"812"}
{"name":"15","number":"823"}
{"name":"16","number":"82"}
{"name":"17","number":"81"}
Answer the question
In order to leave comments, you need to log in
Is the format correct for the json that I am saving?
Or do I need to redo it somehow? (If redo, then how)
<?php
$arr = ['name' => $_POST['name'], 'number' => $_POST['number']];
//читаем данные из файла
$r_json = file_get_contents("data.json");
$arr_json = json_decode($r_json, true);
//склеить данные из файла и теми что получили из формы
$result = array_merge($arr_json, $arr);
//сохраняем данные в файл
$fp = fopen('data.json', 'a');
fwrite($fp, $json_data);
fclose($fp);
header("Content-type: application/json; charset=utf-8");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question