$dataFile = 'data/dane.json';
function loadData($file) {
if (!file_exists($file)) {
$json = file_get_contents($file);
return json_decode($json, true) ?? [];
function saveData($file, $data) {
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$nazwa = trim($_POST['nazwa'] ?? '');
$errors[] = 'Pole nazwa jest wymagane';
// Zapis jeśli brak błędów
$data = loadData($dataFile);
'data' => date('Y-m-d H:i:s')
saveData($dataFile, $data);
$success = 'Dane zostały zapisane!';
// Odczyt danych do wyświetlenia
$items = loadData($dataFile);
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Miniprojekt</title>
<link rel="stylesheet" href="css/style.css">
<p class="success"><?= htmlspecialchars($success) ?></p>
<?php if (!empty($errors)): ?>
<?php foreach ($errors as $error): ?>
<li><?= htmlspecialchars($error) ?></li>
<form method="post" id="mainForm">
<label for="nazwa">Nazwa:</label>
<input type="text" id="nazwa" name="nazwa" required>
<button type="submit">Zapisz</button>
<?php if (empty($items)): ?>
<?php foreach ($items as $item): ?>
<li><?= htmlspecialchars($item['nazwa']) ?> (<?= htmlspecialchars($item['data']) ?>)</li>
<script src="js/app.js"></script>