126 lines
4.3 KiB
PHP
126 lines
4.3 KiB
PHP
<?php
|
|
// Formularverarbeitung und Daten speichern
|
|
if (isset($_POST['save'])) {
|
|
$errors = [];
|
|
|
|
// Validierung der Eingabefelder
|
|
if (empty($_POST['id'])) {
|
|
$errors[] = 'ID ist erforderlich.';
|
|
}
|
|
if (empty($_POST['name'])) {
|
|
$errors[] = 'Name ist erforderlich.';
|
|
}
|
|
if (empty($_POST['wissenschaftlicher_name'])) {
|
|
$errors[] = 'Wissenschaftlicher Name ist erforderlich.';
|
|
}
|
|
if (empty($_POST['beschreibung'])) {
|
|
$errors[] = 'Beschreibung ist erforderlich.';
|
|
}
|
|
if (empty($_POST['lebensraum'])) {
|
|
$errors[] = 'Lebensraum ist erforderlich.';
|
|
}
|
|
if (empty($_POST['nahrung'])) {
|
|
$errors[] = 'Nahrung ist erforderlich.';
|
|
}
|
|
|
|
if (!empty($errors)) {
|
|
foreach ($errors as $error) {
|
|
echo "<p style='color:red;'>{$error}</p>";
|
|
}
|
|
exit;
|
|
}
|
|
|
|
// Eingabedaten bereinigen
|
|
$input = array(
|
|
'id' => htmlspecialchars($_POST['id'], ENT_QUOTES, 'UTF-8'),
|
|
'name' => htmlspecialchars($_POST['name'], ENT_QUOTES, 'UTF-8'),
|
|
'wissenschaftlicher_name' => htmlspecialchars($_POST['wissenschaftlicher_name'], ENT_QUOTES, 'UTF-8'),
|
|
'beschreibung' => htmlspecialchars($_POST['beschreibung'], ENT_QUOTES, 'UTF-8'),
|
|
'lebensraum' => htmlspecialchars($_POST['lebensraum'], ENT_QUOTES, 'UTF-8'),
|
|
'nahrung' => htmlspecialchars($_POST['nahrung'], ENT_QUOTES, 'UTF-8')
|
|
);
|
|
|
|
// JSON-Datei öffnen
|
|
$data = file_get_contents('tiere.json');
|
|
$data = json_decode($data, true);
|
|
|
|
if ($data === null) {
|
|
$data = [];
|
|
}
|
|
|
|
// Neue Daten hinzufügen
|
|
$data[] = $input;
|
|
|
|
// JSON-Datei speichern
|
|
$data = json_encode($data, JSON_PRETTY_PRINT);
|
|
if (file_put_contents('tiere.json', $data) === false) {
|
|
echo "<p style='color:red;'>Fehler beim Speichern der Daten.</p>";
|
|
exit;
|
|
}
|
|
|
|
// Umleitung
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Tiere in unserem Kleingarten</title>
|
|
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css">
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h1 class="page-header text-center">Tiere in unserem Kleingarten</h1>
|
|
<div class="row">
|
|
<div class="col-1"></div>
|
|
<div class="col-8"><a href="index.php" class="btn btn-primary">Zurück</a>
|
|
<form method="POST">
|
|
<div class="mb-3 row">
|
|
<label class="col-sm-2 col-form-label">ID</label>
|
|
<div class="col-sm-10">
|
|
<input type="text" class="form-control" id="id" name="id" required>
|
|
</div>
|
|
</div>
|
|
<div class="mb-3 row">
|
|
<label class="col-sm-2 col-form-label">Name</label>
|
|
<div class="col-sm-10">
|
|
<input type="text" class="form-control" id="name" name="name" required>
|
|
</div>
|
|
</div>
|
|
<div class="mb-3 row">
|
|
<label class="col-sm-2 col-form-label">Wissenschaftlicher Name</label>
|
|
<div class="col-sm-10">
|
|
<input type="text" class="form-control" id="wissenschaftlicher_name" name="wissenschaftlicher_name" required>
|
|
</div>
|
|
</div>
|
|
<div class="mb-3 row">
|
|
<label class="col-sm-2 col-form-label">Beschreibung</label>
|
|
<div class="col-sm-10">
|
|
<input type="text" class="form-control" id="beschreibung" name="beschreibung" required>
|
|
</div>
|
|
</div>
|
|
<div class="mb-3 row">
|
|
<label class="col-sm-2 col-form-label">Lebensraum</label>
|
|
<div class="col-sm-10">
|
|
<input type="text" class="form-control" id="lebensraum" name="lebensraum" required>
|
|
</div>
|
|
</div>
|
|
<div class="mb-3 row">
|
|
<label class="col-sm-2 col-form-label">Nahrung</label>
|
|
<div class="col-sm-10">
|
|
<input type="text" class="form-control" id="nahrung" name="nahrung" required>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<input type="submit" name="save" value="Speichern" class="btn btn-primary" required>
|
|
</form>
|
|
</div>
|
|
<div class="col-5"></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|