Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- i want to make a simple blog with 3 php functions to save load and delete a blogpost but my php isnt working, i always got a 404 error
- php load_post:
- ```
- <?php
- // Beispiel: Laden von Beiträgen aus einer Datei
- $file = 'posts.txt';
- // Überprüfe, ob die Datei existiert
- if (file_exists($file)) {
- // Lese den Inhalt der Datei
- $posts_content = file_get_contents($file);
- // Wandele den Inhalt in ein PHP-Array um (jede Zeile enthält ein JSON-Objekt)
- $posts_lines = explode("\n", trim($posts_content));
- $posts = [];
- foreach ($posts_lines as $line) {
- if (!empty($line)) {
- $post = json_decode($line, true);
- $posts[] = $post;
- }
- }
- // Gebe die Beiträge als JSON zurück
- header('Content-Type: application/json');
- echo json_encode($posts);
- } else {
- // Wenn die Datei nicht existiert, gebe einen leeren JSON-Array zurück
- echo json_encode([]);
- }
- ?>
- ```
- delete_post.php
- ```
- <?php
- // Beispiel: Löschen eines Beitrags aus einer Datei
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- // Empfange und dekodiere JSON-Daten
- $post_data = json_decode(file_get_contents('php://input'), true);
- // Überprüfe, ob die ID des zu löschenden Beitrags gesetzt ist
- if (isset($post_data['id'])) {
- $id_to_delete = $post_data['id'];
- // Lese vorhandene Beiträge aus der Datei
- $file = 'posts.txt';
- $current_data = file_get_contents($file);
- $posts = explode("\n", trim($current_data));
- // Filtere den zu löschenden Beitrag aus der Liste
- $updated_posts = [];
- foreach ($posts as $post) {
- if (!empty($post)) {
- $decoded_post = json_decode($post, true);
- if ($decoded_post['id'] != $id_to_delete) {
- $updated_posts[] = $post;
- }
- }
- }
- // Speichere die aktualisierten Beiträge zurück in die Datei
- file_put_contents($file, implode("\n", $updated_posts) . "\n");
- // Erfolgreiche Antwort zurückgeben
- http_response_code(200);
- echo json_encode(['message' => 'Post erfolgreich gelöscht.']);
- } else {
- // Fehlerhafte Anfrage
- http_response_code(400);
- echo json_encode(['message' => 'Fehler: ID des zu löschenden Posts nicht angegeben.']);
- }
- } else {
- // Methode nicht erlaubt
- http_response_code(405);
- echo json_encode(['message' => 'Methode nicht erlaubt.']);
- }
- ?>
- ```
- save_post.php
- ```
- <?php
- // Überprüfe, ob POST-Daten gesendet wurden
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- // Empfange und dekodiere JSON-Daten
- $post_data = json_decode(file_get_contents('php://input'), true);
- // Überprüfe, ob Titel und Inhalt gesetzt sind
- if (isset($post_data['title']) && isset($post_data['content'])) {
- $title = $post_data['title'];
- $content = $post_data['content'];
- // Hier könntest du den Beitrag in einer Datei oder Datenbank speichern
- // Beispiel für das Speichern in einer Datei (posts.txt)
- $file = 'posts.txt';
- $current_data = file_get_contents($file);
- $new_post = [
- 'title' => $title,
- 'content' => $content
- ];
- $current_data .= json_encode($new_post) . "\n";
- file_put_contents($file, $current_data);
- // Erfolgreiche Antwort zurückgeben
- http_response_code(200);
- echo json_encode(['message' => 'Post erfolgreich gespeichert.']);
- } else {
- // Fehlerhafte Anfrage
- http_response_code(400);
- echo json_encode(['message' => 'Fehler: Titel und Inhalt müssen angegeben werden.']);
- }
- } else {
- // Methode nicht erlaubt
- http_response_code(405);
- echo json_encode(['message' => 'Methode nicht erlaubt.']);
- }
- ?>
- ```
- save_post.php
- i tried changing the names but id didint help
- index.hmtl
- <!DOCTYPE html>
- <html lang="de">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Warhammer 40k Universum</title>
- <style>
- body {
- font-family: Arial, sans-serif;
- background-color: #1a1a1a;
- color: #f0f0f0;
- margin: 0;
- padding: 0;
- }
- header, footer {
- background-color: #333;
- padding: 1em;
- text-align: center;
- }
- nav {
- background-color: #444;
- padding: 1em;
- text-align: center;
- }
- nav a {
- color: #f0f0f0;
- margin: 0 1em;
- text-decoration: none;
- }
- section {
- padding: 2em;
- }
- .container {
- max-width: 1200px;
- margin: 0 auto;
- }
- .blog-post {
- background-color: #2a2a2a;
- padding: 1em;
- margin: 1em 0;
- border-radius: 5px;
- position: relative;
- }
- .blog-post h3 {
- margin-top: 0;
- }
- .blog-post button {
- position: absolute;
- top: 10px;
- right: 10px;
- background-color: #f44336;
- color: #fff;
- border: none;
- border-radius: 3px;
- padding: 0.5em;
- cursor: pointer;
- }
- .add-post-button {
- background-color: #555;
- color: #fff;
- padding: 0.5em 1em;
- border: none;
- cursor: pointer;
- border-radius: 5px;
- margin-bottom: 1em;
- }
- .form-container {
- display: none;
- background-color: #2a2a2a;
- padding: 1em;
- border-radius: 5px;
- margin-bottom: 1em;
- }
- .form-container input, .form-container textarea {
- width: 100%;
- padding: 0.5em;
- margin: 0.5em 0;
- border: 1px solid #555;
- border-radius: 5px;
- background-color: #1a1a1a;
- color: #f0f0f0;
- }
- .form-container button {
- background-color: #555;
- color: #fff;
- padding: 0.5em 1em;
- border: none;
- cursor: pointer;
- border-radius: 5px;
- }
- .category-header {
- cursor: pointer;
- }
- </style>
- </head>
- <body>
- <header>
- <h1>Willkommen im Warhammer 40k Universum</h1>
- </header>
- <nav>
- <a href="#lore">Lore</a>
- <a href="#modelling">Modellbau</a>
- <a href="#gameplay">Spielanleitungen</a>
- <a href="#community">Community</a>
- <a href="#resources">Ressourcen</a>
- </nav>
- <section id="lore" class="container">
- <h2 class="category-header" onclick="toggleCategory('lore')">Lore und Hintergrundgeschichten</h2>
- <button class="add-post-button" onclick="showForm('lore')">+ Beitrag hinzufügen</button>
- <div class="form-container" id="lore-form">
- <h3>Neuen Beitrag hinzufügen</h3>
- <input type="text" id="lore-title" placeholder="Titel">
- <textarea id="lore-content" placeholder="Inhalt"></textarea>
- <button onclick="addPost('lore')">Hinzufügen</button>
- </div>
- <div id="lore-posts">
- <!-- Blog posts will be inserted here -->
- </div>
- </section>
- <section id="modelling" class="container">
- <h2 class="category-header" onclick="toggleCategory('modelling')">Modellbau und Bemalung</h2>
- <button class="add-post-button" onclick="showForm('modelling')">+ Beitrag hinzufügen</button>
- <div class="form-container" id="modelling-form">
- <h3>Neuen Beitrag hinzufügen</h3>
- <input type="text" id="modelling-title" placeholder="Titel">
- <textarea id="modelling-content" placeholder="Inhalt"></textarea>
- <button onclick="addPost('modelling')">Hinzufügen</button>
- </div>
- <div id="modelling-posts">
- <!-- Blog posts will be inserted here -->
- </div>
- </section>
- <section id="gameplay" class="container">
- <h2 class="category-header" onclick="toggleCategory('gameplay')">Spielanleitungen und Strategien</h2>
- <button class="add-post-button" onclick="showForm('gameplay')">+ Beitrag hinzufügen</button>
- <div class="form-container" id="gameplay-form">
- <h3>Neuen Beitrag hinzufügen</h3>
- <input type="text" id="gameplay-title" placeholder="Titel">
- <textarea id="gameplay-content" placeholder="Inhalt"></textarea>
- <button onclick="addPost('gameplay')">Hinzufügen</button>
- </div>
- <div id="gameplay-posts">
- <!-- Blog posts will be inserted here -->
- </div>
- </section>
- <section id="community" class="container">
- <h2 class="category-header" onclick="toggleCategory('community')">Community und Events</h2>
- <button class="add-post-button" onclick="showForm('community')">+ Beitrag hinzufügen</button>
- <div class="form-container" id="community-form">
- <h3>Neuen Beitrag hinzufügen</h3>
- <input type="text" id="community-title" placeholder="Titel">
- <textarea id="community-content" placeholder="Inhalt"></textarea>
- <button onclick="addPost('community')">Hinzufügen</button>
- </div>
- <div id="community-posts">
- <!-- Blog posts will be inserted here -->
- </div>
- </section>
- <section id="resources" class="container">
- <h2 class="category-header" onclick="toggleCategory('resources')">Ressourcen und Downloads</h2>
- <button class="add-post-button" onclick="showForm('resources')">+ Beitrag hinzufügen</button>
- <div class="form-container" id="resources-form">
- <h3>Neuen Beitrag hinzufügen</h3>
- <input type="text" id="resources-title" placeholder="Titel">
- <textarea id="resources-content" placeholder="Inhalt"></textarea>
- <button onclick="addPost('resources')">Hinzufügen</button>
- </div>
- <div id="resources-posts">
- <!-- Blog posts will be inserted here -->
- </div>
- </section>
- <footer>
- <p>© 2024 Warhammer 40k Universum. Alle Rechte vorbehalten.</p>
- </footer>
- <script>
- document.addEventListener('DOMContentLoaded', loadPosts);
- function showForm(category) {
- document.getElementById(category + '-form').style.display = 'block';
- }
- function addPost(category) {
- const title = document.getElementById(category + '-title').value;
- const content = document.getElementById(category + '-content').value;
- if (title && content) {
- const post = { id: Date.now(), category, title, content };
- savePost(post);
- appendPost(post);
- // Clear the form
- document.getElementById(category + '-title').value = '';
- document.getElementById(category + '-content').value = '';
- document.getElementById(category + '-form').style.display = 'none';
- } else {
- alert('Bitte füllen Sie sowohl den Titel als auch den Inhalt aus.');
- }
- }
- function savePost(post) {
- const xhr = new XMLHttpRequest();
- xhr.open('POST', 'php/save_post.php', true); // Passe den Pfad entsprechend deiner Ordnerstruktur an
- xhr.setRequestHeader('Content-Type', 'application/json');
- xhr.onreadystatechange = function() {
- if (xhr.readyState === XMLHttpRequest.DONE) {
- if (xhr.status === 200) {
- console.log('Post erfolgreich gespeichert:', xhr.responseText);
- } else {
- console.error('Fehler beim Speichern des Posts:', xhr.status);
- alert('Fehler beim Speichern des Posts. Bitte versuchen Sie es erneut.');
- }
- }
- };
- xhr.send(JSON.stringify(post));
- }
- function loadPosts() {
- const xhr = new XMLHttpRequest();
- xhr.open('GET', 'php/load_post.php', true); // Passe den Pfad entsprechend deiner Ordnerstruktur an
- xhr.onreadystatechange = function() {
- if (xhr.readyState === XMLHttpRequest.DONE) {
- if (xhr.status === 200) {
- const posts = JSON.parse(xhr.responseText);
- posts.forEach(post => appendPost(post));
- } else {
- console.error('Fehler beim Laden der Posts:', xhr.status);
- }
- }
- };
- xhr.send();
- }
- function appendPost(post) {
- const postElement = document.createElement('div');
- postElement.classList.add('blog-post');
- postElement.setAttribute('data-id', post.id);
- postElement.innerHTML = `
- <h3>${post.title}</h3>
- <p>${post.content}</p>
- <button onclick="deletePost(${post.id})">Löschen</button>
- `;
- document.getElementById(post.category + '-posts').appendChild(postElement);
- }
- function deletePost(id) {
- const xhr = new XMLHttpRequest();
- xhr.open('POST', 'php/delete_post.php', true); // Passe den Pfad entsprechend deiner Ordnerstruktur an
- xhr.setRequestHeader('Content-Type', 'application/json');
- xhr.onreadystatechange = function() {
- if (xhr.readyState === XMLHttpRequest.DONE) {
- if (xhr.status === 200) {
- console.log('Post erfolgreich gelöscht:', xhr.responseText);
- // Aktualisiere die Anzeige nach dem Löschen des Posts
- const postElement = document.querySelector(`.blog-post[data-id="${id}"]`);
- if (postElement) {
- postElement.remove();
- }
- } else {
- console.error('Fehler beim Löschen des Posts:', xhr.status);
- alert('Fehler beim Löschen des Posts. Bitte versuchen Sie es erneut.');
- }
- }
- };
- xhr.send(JSON.stringify({ id }));
- }
- function toggleCategory(category) {
- const postsContainer = document.getElementById(category + '-posts');
- postsContainer.style.display = postsContainer.style.display === 'none' ? 'block' : 'none';
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement