Advertisement
Guest User

php error

a guest
Jun 28th, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.80 KB | None | 0 0
  1. 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
  2.  
  3.  
  4.  
  5. php load_post:
  6. ```
  7. <?php
  8. // Beispiel: Laden von Beiträgen aus einer Datei
  9. $file = 'posts.txt';
  10.  
  11. // Überprüfe, ob die Datei existiert
  12. if (file_exists($file)) {
  13. // Lese den Inhalt der Datei
  14. $posts_content = file_get_contents($file);
  15.  
  16. // Wandele den Inhalt in ein PHP-Array um (jede Zeile enthält ein JSON-Objekt)
  17. $posts_lines = explode("\n", trim($posts_content));
  18. $posts = [];
  19.  
  20. foreach ($posts_lines as $line) {
  21. if (!empty($line)) {
  22. $post = json_decode($line, true);
  23. $posts[] = $post;
  24. }
  25. }
  26.  
  27. // Gebe die Beiträge als JSON zurück
  28. header('Content-Type: application/json');
  29. echo json_encode($posts);
  30. } else {
  31. // Wenn die Datei nicht existiert, gebe einen leeren JSON-Array zurück
  32. echo json_encode([]);
  33. }
  34. ?>
  35.  
  36. ```
  37. delete_post.php
  38. ```
  39. <?php
  40. // Beispiel: Löschen eines Beitrags aus einer Datei
  41. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  42. // Empfange und dekodiere JSON-Daten
  43. $post_data = json_decode(file_get_contents('php://input'), true);
  44.  
  45. // Überprüfe, ob die ID des zu löschenden Beitrags gesetzt ist
  46. if (isset($post_data['id'])) {
  47. $id_to_delete = $post_data['id'];
  48.  
  49. // Lese vorhandene Beiträge aus der Datei
  50. $file = 'posts.txt';
  51. $current_data = file_get_contents($file);
  52. $posts = explode("\n", trim($current_data));
  53.  
  54. // Filtere den zu löschenden Beitrag aus der Liste
  55. $updated_posts = [];
  56. foreach ($posts as $post) {
  57. if (!empty($post)) {
  58. $decoded_post = json_decode($post, true);
  59. if ($decoded_post['id'] != $id_to_delete) {
  60. $updated_posts[] = $post;
  61. }
  62. }
  63. }
  64.  
  65. // Speichere die aktualisierten Beiträge zurück in die Datei
  66. file_put_contents($file, implode("\n", $updated_posts) . "\n");
  67.  
  68. // Erfolgreiche Antwort zurückgeben
  69. http_response_code(200);
  70. echo json_encode(['message' => 'Post erfolgreich gelöscht.']);
  71. } else {
  72. // Fehlerhafte Anfrage
  73. http_response_code(400);
  74. echo json_encode(['message' => 'Fehler: ID des zu löschenden Posts nicht angegeben.']);
  75. }
  76. } else {
  77. // Methode nicht erlaubt
  78. http_response_code(405);
  79. echo json_encode(['message' => 'Methode nicht erlaubt.']);
  80. }
  81. ?>
  82.  
  83. ```
  84. save_post.php
  85. ```
  86. <?php
  87. // Überprüfe, ob POST-Daten gesendet wurden
  88. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  89. // Empfange und dekodiere JSON-Daten
  90. $post_data = json_decode(file_get_contents('php://input'), true);
  91.  
  92. // Überprüfe, ob Titel und Inhalt gesetzt sind
  93. if (isset($post_data['title']) && isset($post_data['content'])) {
  94. $title = $post_data['title'];
  95. $content = $post_data['content'];
  96.  
  97. // Hier könntest du den Beitrag in einer Datei oder Datenbank speichern
  98. // Beispiel für das Speichern in einer Datei (posts.txt)
  99. $file = 'posts.txt';
  100. $current_data = file_get_contents($file);
  101. $new_post = [
  102. 'title' => $title,
  103. 'content' => $content
  104. ];
  105. $current_data .= json_encode($new_post) . "\n";
  106. file_put_contents($file, $current_data);
  107.  
  108. // Erfolgreiche Antwort zurückgeben
  109. http_response_code(200);
  110. echo json_encode(['message' => 'Post erfolgreich gespeichert.']);
  111. } else {
  112. // Fehlerhafte Anfrage
  113. http_response_code(400);
  114. echo json_encode(['message' => 'Fehler: Titel und Inhalt müssen angegeben werden.']);
  115. }
  116. } else {
  117. // Methode nicht erlaubt
  118. http_response_code(405);
  119. echo json_encode(['message' => 'Methode nicht erlaubt.']);
  120. }
  121. ?>
  122.  
  123. ```
  124.  
  125. save_post.php
  126.  
  127.  
  128.  
  129.  
  130. i tried changing the names but id didint help
  131.  
  132. index.hmtl
  133.  
  134. <!DOCTYPE html>
  135. <html lang="de">
  136. <head>
  137. <meta charset="UTF-8">
  138. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  139. <title>Warhammer 40k Universum</title>
  140. <style>
  141. body {
  142. font-family: Arial, sans-serif;
  143. background-color: #1a1a1a;
  144. color: #f0f0f0;
  145. margin: 0;
  146. padding: 0;
  147. }
  148. header, footer {
  149. background-color: #333;
  150. padding: 1em;
  151. text-align: center;
  152. }
  153. nav {
  154. background-color: #444;
  155. padding: 1em;
  156. text-align: center;
  157. }
  158. nav a {
  159. color: #f0f0f0;
  160. margin: 0 1em;
  161. text-decoration: none;
  162. }
  163. section {
  164. padding: 2em;
  165. }
  166. .container {
  167. max-width: 1200px;
  168. margin: 0 auto;
  169. }
  170. .blog-post {
  171. background-color: #2a2a2a;
  172. padding: 1em;
  173. margin: 1em 0;
  174. border-radius: 5px;
  175. position: relative;
  176. }
  177. .blog-post h3 {
  178. margin-top: 0;
  179. }
  180. .blog-post button {
  181. position: absolute;
  182. top: 10px;
  183. right: 10px;
  184. background-color: #f44336;
  185. color: #fff;
  186. border: none;
  187. border-radius: 3px;
  188. padding: 0.5em;
  189. cursor: pointer;
  190. }
  191. .add-post-button {
  192. background-color: #555;
  193. color: #fff;
  194. padding: 0.5em 1em;
  195. border: none;
  196. cursor: pointer;
  197. border-radius: 5px;
  198. margin-bottom: 1em;
  199. }
  200. .form-container {
  201. display: none;
  202. background-color: #2a2a2a;
  203. padding: 1em;
  204. border-radius: 5px;
  205. margin-bottom: 1em;
  206. }
  207. .form-container input, .form-container textarea {
  208. width: 100%;
  209. padding: 0.5em;
  210. margin: 0.5em 0;
  211. border: 1px solid #555;
  212. border-radius: 5px;
  213. background-color: #1a1a1a;
  214. color: #f0f0f0;
  215. }
  216. .form-container button {
  217. background-color: #555;
  218. color: #fff;
  219. padding: 0.5em 1em;
  220. border: none;
  221. cursor: pointer;
  222. border-radius: 5px;
  223. }
  224. .category-header {
  225. cursor: pointer;
  226. }
  227. </style>
  228. </head>
  229. <body>
  230. <header>
  231. <h1>Willkommen im Warhammer 40k Universum</h1>
  232. </header>
  233. <nav>
  234. <a href="#lore">Lore</a>
  235. <a href="#modelling">Modellbau</a>
  236. <a href="#gameplay">Spielanleitungen</a>
  237. <a href="#community">Community</a>
  238. <a href="#resources">Ressourcen</a>
  239. </nav>
  240. <section id="lore" class="container">
  241. <h2 class="category-header" onclick="toggleCategory('lore')">Lore und Hintergrundgeschichten</h2>
  242. <button class="add-post-button" onclick="showForm('lore')">+ Beitrag hinzufügen</button>
  243. <div class="form-container" id="lore-form">
  244. <h3>Neuen Beitrag hinzufügen</h3>
  245. <input type="text" id="lore-title" placeholder="Titel">
  246. <textarea id="lore-content" placeholder="Inhalt"></textarea>
  247. <button onclick="addPost('lore')">Hinzufügen</button>
  248. </div>
  249. <div id="lore-posts">
  250. <!-- Blog posts will be inserted here -->
  251. </div>
  252. </section>
  253. <section id="modelling" class="container">
  254. <h2 class="category-header" onclick="toggleCategory('modelling')">Modellbau und Bemalung</h2>
  255. <button class="add-post-button" onclick="showForm('modelling')">+ Beitrag hinzufügen</button>
  256. <div class="form-container" id="modelling-form">
  257. <h3>Neuen Beitrag hinzufügen</h3>
  258. <input type="text" id="modelling-title" placeholder="Titel">
  259. <textarea id="modelling-content" placeholder="Inhalt"></textarea>
  260. <button onclick="addPost('modelling')">Hinzufügen</button>
  261. </div>
  262. <div id="modelling-posts">
  263. <!-- Blog posts will be inserted here -->
  264. </div>
  265. </section>
  266. <section id="gameplay" class="container">
  267. <h2 class="category-header" onclick="toggleCategory('gameplay')">Spielanleitungen und Strategien</h2>
  268. <button class="add-post-button" onclick="showForm('gameplay')">+ Beitrag hinzufügen</button>
  269. <div class="form-container" id="gameplay-form">
  270. <h3>Neuen Beitrag hinzufügen</h3>
  271. <input type="text" id="gameplay-title" placeholder="Titel">
  272. <textarea id="gameplay-content" placeholder="Inhalt"></textarea>
  273. <button onclick="addPost('gameplay')">Hinzufügen</button>
  274. </div>
  275. <div id="gameplay-posts">
  276. <!-- Blog posts will be inserted here -->
  277. </div>
  278. </section>
  279. <section id="community" class="container">
  280. <h2 class="category-header" onclick="toggleCategory('community')">Community und Events</h2>
  281. <button class="add-post-button" onclick="showForm('community')">+ Beitrag hinzufügen</button>
  282. <div class="form-container" id="community-form">
  283. <h3>Neuen Beitrag hinzufügen</h3>
  284. <input type="text" id="community-title" placeholder="Titel">
  285. <textarea id="community-content" placeholder="Inhalt"></textarea>
  286. <button onclick="addPost('community')">Hinzufügen</button>
  287. </div>
  288. <div id="community-posts">
  289. <!-- Blog posts will be inserted here -->
  290. </div>
  291. </section>
  292. <section id="resources" class="container">
  293. <h2 class="category-header" onclick="toggleCategory('resources')">Ressourcen und Downloads</h2>
  294. <button class="add-post-button" onclick="showForm('resources')">+ Beitrag hinzufügen</button>
  295. <div class="form-container" id="resources-form">
  296. <h3>Neuen Beitrag hinzufügen</h3>
  297. <input type="text" id="resources-title" placeholder="Titel">
  298. <textarea id="resources-content" placeholder="Inhalt"></textarea>
  299. <button onclick="addPost('resources')">Hinzufügen</button>
  300. </div>
  301. <div id="resources-posts">
  302. <!-- Blog posts will be inserted here -->
  303. </div>
  304. </section>
  305. <footer>
  306. <p>&copy; 2024 Warhammer 40k Universum. Alle Rechte vorbehalten.</p>
  307. </footer>
  308. <script>
  309. document.addEventListener('DOMContentLoaded', loadPosts);
  310.  
  311. function showForm(category) {
  312. document.getElementById(category + '-form').style.display = 'block';
  313. }
  314.  
  315. function addPost(category) {
  316. const title = document.getElementById(category + '-title').value;
  317. const content = document.getElementById(category + '-content').value;
  318.  
  319. if (title && content) {
  320. const post = { id: Date.now(), category, title, content };
  321. savePost(post);
  322. appendPost(post);
  323.  
  324. // Clear the form
  325. document.getElementById(category + '-title').value = '';
  326. document.getElementById(category + '-content').value = '';
  327. document.getElementById(category + '-form').style.display = 'none';
  328. } else {
  329. alert('Bitte füllen Sie sowohl den Titel als auch den Inhalt aus.');
  330. }
  331. }
  332.  
  333. function savePost(post) {
  334. const xhr = new XMLHttpRequest();
  335. xhr.open('POST', 'php/save_post.php', true); // Passe den Pfad entsprechend deiner Ordnerstruktur an
  336. xhr.setRequestHeader('Content-Type', 'application/json');
  337.  
  338. xhr.onreadystatechange = function() {
  339. if (xhr.readyState === XMLHttpRequest.DONE) {
  340. if (xhr.status === 200) {
  341. console.log('Post erfolgreich gespeichert:', xhr.responseText);
  342. } else {
  343. console.error('Fehler beim Speichern des Posts:', xhr.status);
  344. alert('Fehler beim Speichern des Posts. Bitte versuchen Sie es erneut.');
  345. }
  346. }
  347. };
  348.  
  349. xhr.send(JSON.stringify(post));
  350. }
  351.  
  352. function loadPosts() {
  353. const xhr = new XMLHttpRequest();
  354. xhr.open('GET', 'php/load_post.php', true); // Passe den Pfad entsprechend deiner Ordnerstruktur an
  355.  
  356. xhr.onreadystatechange = function() {
  357. if (xhr.readyState === XMLHttpRequest.DONE) {
  358. if (xhr.status === 200) {
  359. const posts = JSON.parse(xhr.responseText);
  360. posts.forEach(post => appendPost(post));
  361. } else {
  362. console.error('Fehler beim Laden der Posts:', xhr.status);
  363. }
  364. }
  365. };
  366.  
  367. xhr.send();
  368. }
  369.  
  370. function appendPost(post) {
  371. const postElement = document.createElement('div');
  372. postElement.classList.add('blog-post');
  373. postElement.setAttribute('data-id', post.id);
  374. postElement.innerHTML = `
  375. <h3>${post.title}</h3>
  376. <p>${post.content}</p>
  377. <button onclick="deletePost(${post.id})">Löschen</button>
  378. `;
  379. document.getElementById(post.category + '-posts').appendChild(postElement);
  380. }
  381.  
  382. function deletePost(id) {
  383. const xhr = new XMLHttpRequest();
  384. xhr.open('POST', 'php/delete_post.php', true); // Passe den Pfad entsprechend deiner Ordnerstruktur an
  385. xhr.setRequestHeader('Content-Type', 'application/json');
  386.  
  387. xhr.onreadystatechange = function() {
  388. if (xhr.readyState === XMLHttpRequest.DONE) {
  389. if (xhr.status === 200) {
  390. console.log('Post erfolgreich gelöscht:', xhr.responseText);
  391. // Aktualisiere die Anzeige nach dem Löschen des Posts
  392. const postElement = document.querySelector(`.blog-post[data-id="${id}"]`);
  393. if (postElement) {
  394. postElement.remove();
  395. }
  396. } else {
  397. console.error('Fehler beim Löschen des Posts:', xhr.status);
  398. alert('Fehler beim Löschen des Posts. Bitte versuchen Sie es erneut.');
  399. }
  400. }
  401. };
  402.  
  403. xhr.send(JSON.stringify({ id }));
  404. }
  405.  
  406. function toggleCategory(category) {
  407. const postsContainer = document.getElementById(category + '-posts');
  408. postsContainer.style.display = postsContainer.style.display === 'none' ? 'block' : 'none';
  409. }
  410. </script>
  411. </body>
  412. </html>
  413.  
  414.  
  415.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement