diff --git a/api/opfer/delete.php b/api/opfer/delete.php new file mode 100644 index 0000000..c701518 --- /dev/null +++ b/api/opfer/delete.php @@ -0,0 +1,31 @@ +checkAuth()) { + header("Location: /index.html?status=500&error=Unauthorized"); + exit; +} + +if (!isset($_POST["id"])) { + die('{"status":500, "error":"Missing fields"}'); +} + +$id = intval($_POST["id"]); + +$db = $api->getDB(); + +$sql = "DELETE FROM opfer WHERE id = :id"; + +$stmt = $db->prepare($sql); + +$sql_exec = $stmt->execute([':id' => $id]); + +if ($sql_exec) { + header("Location: /index.html?status=200"); +}else{ + header("Location: /index.html?status=500&error=SQL Error"); +} +exit; +?> \ No newline at end of file diff --git a/api/opfer/edit.php b/api/opfer/edit.php new file mode 100644 index 0000000..78b72e7 --- /dev/null +++ b/api/opfer/edit.php @@ -0,0 +1,44 @@ +checkAuth()) { + header("Location: /index.html?status=500&error=Unauthorized"); + exit; +} + +if (!isset($_POST["id"]) || !isset($_POST["name"]) || !isset($_POST["mail"]) || !isset($_POST["number"])) { + die('{"status":500, "error":"Missing fields"}'); +} + +$name = htmlspecialchars($_POST["name"]); +$mail = htmlspecialchars($_POST["mail"]); +$number = intval(htmlspecialchars($_POST["number"])); +echo $number; +$id = intval(htmlspecialchars($_POST["id"])); +if (isset($_POST["enabled2"])) { + $enabled = 1; +}else{ + $enabled = 0; +} + +$db = $api->getDB(); + +$sql = "UPDATE opfer SET name = :name, mail = :mail, number = :number, status = :status WHERE id = :id"; + +$stmt = $db->prepare($sql); + +$sql_exec = $stmt->execute([':name' => $name, ':mail' => $mail, ':number' => $number, ':status' => $enabled, ':id' => $id]); + +if ($sql_exec) { + header("Location: /index.html?status=200"); +}else{ + header("Location: /index.html?status=500&error=SQL Error"); +} +exit; +?> \ No newline at end of file diff --git a/api/opfer/info.php b/api/opfer/info.php index 6cfade3..36fa685 100644 --- a/api/opfer/info.php +++ b/api/opfer/info.php @@ -19,7 +19,7 @@ $sql = "SELECT * FROM opfer WHERE id = :id"; $stmt = $db->prepare($sql); $stmt->execute([':id' => $id]); -$result = $stmt->fetchAll(PDO::FETCH_ASSOC); +$result = $stmt->fetch(PDO::FETCH_ASSOC); if ($result) { $json["opfer"] = $result; diff --git a/front/basic-tables.html b/front/basic-tables.html index 90b20af..8c5dce8 100644 --- a/front/basic-tables.html +++ b/front/basic-tables.html @@ -665,7 +665,7 @@ - + + function opferEdit(id) { + console.log(document.getElementById("opfer_name")); + fetch("/api/opfer/info.php", { + method: "POST", + headers: { + "Content-Type": "application/x-www-form-urlencoded" + }, + body: "id=" + id + }) + .then(response => response.json()) + .then(data => { + if (data.status == 500) { + console.error("API Error: " + data.error); + } + + const opfer = data.opfer; + + document.getElementById("opfer_id").value = id; + document.getElementById("opfer_name").value = opfer.name; + document.getElementById("opfer_mail").value = opfer.mail; + document.getElementById("opfer_number").value = opfer.number; + + + if (data.status == 1) { + const toogle_el = document.querySelector('#toggle2'); + const toogle_data = Alpine.$data(document.getElementById("toogle2_div")); + + toogle_data.switcherToggle = data.status; + toogle_el.checked = data.status; + } + }) + } + + function removeOpfer() { + const id = document.getElementById("opfer_id").value; + + fetch("/api/opfer/delete.php", { + method: "POST", + headers: { + "Content-Type": "application/x-www-form-urlencoded" + }, + body: "id=" + id + }) + window.location.reload(); + } + ---> diff --git a/front/index.html b/front/index.html index 20a349d..c4d44ac 100644 --- a/front/index.html +++ b/front/index.html @@ -639,7 +639,7 @@
- Das neue Opfer wurde erfolgreich hinzugefügt! + Die Aktion wurde erfolgreich ausgeführt
@@ -751,7 +751,7 @@ - + + function opferEdit(id) { + console.log(document.getElementById("opfer_name")); + fetch("/api/opfer/info.php", { + method: "POST", + headers: { + "Content-Type": "application/x-www-form-urlencoded" + }, + body: "id=" + id + }) + .then(response => response.json()) + .then(data => { + if (data.status == 500) { + console.error("API Error: " + data.error); + } + + const opfer = data.opfer; + + document.getElementById("opfer_id").value = id; + document.getElementById("opfer_name").value = opfer.name; + document.getElementById("opfer_mail").value = opfer.mail; + document.getElementById("opfer_number").value = opfer.number; + + + if (data.status == 1) { + const toogle_el = document.querySelector('#toggle2'); + const toogle_data = Alpine.$data(document.getElementById("toogle2_div")); + + toogle_data.switcherToggle = data.status; + toogle_el.checked = data.status; + } + }) + } + + function removeOpfer() { + const id = document.getElementById("opfer_id").value; + + fetch("/api/opfer/delete.php", { + method: "POST", + headers: { + "Content-Type": "application/x-www-form-urlencoded" + }, + body: "id=" + id + }) + window.location.reload(); + } + ---> @@ -1014,6 +1061,7 @@ -