<?php
//schreiben
$fh = fopen("telefonliste.json", "a");
$txt = file_get_contents("telefonliste.json");
if($txt == "" or $txt == null) {
$list = array();
} else {
$list = json_decode($txt, true);
}
if(isset($_POST['Name'])) {
$list[] = array('Name' => $_POST['Name'],
'Telefonnummer' => $_POST['Telefonnummer']);
}
$txt = json_encode($list);
file_put_contents("telefonliste.json", $txt);
if(isset($_GET["delete"])) {
$i = $_GET["delete"];
unset($list[$i]);
}
$txt = json_encode($list, true);
file_put_contents("telefonliste.json", $txt);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Telefonliste</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="./css/main.css" />
</head>
<body>
<header>
<h2>Telefonliste</h2>
</header>
<main>
<ul id="todolist">
<?php
//lesen
$txt = file_get_contents('telefonliste.json');
$list = json_decode($txt, true);
$i=0;
foreach ($list as $entry) {
echo
'<li>
<a href="index.php?action=mark&id= "class="markiert" title="Kunde ist markiert"></a>
<span>' . $entry['Name'] . '</span>
<span>' . $entry['Telefonnummer'] . '</span>
<a href="index.php?delete=' . $i . '" class="delete" title="löschen">löschen</a>
</li>';
$i++;}
?>
</ul>
<div class="spacer"></div>
<form id="add-todo" action='index.php' method='post'>
<input type='text' placeholder='Name' name='Name'>
<input type='text' placeholder='Telefonnummer' name='Telefonnummer'>
<input type='submit' value='hinzufügen'>
</form>
</main>
</body>
</html>