Liste - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Liste.php

  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title>Liste erstellen, bearbeiten und speichern</title>
  5.   </head>
  6.   <body>
  7.     <h1>Liste erstellen, bearbeiten und speichern</h1>
  8.     <form action="list.php" method="post">
  9.       <label for="item">Eintrag hinzufügen:</label>
  10.       <input type="text" id="item" name="item">
  11.       <input type="submit" name="add" value="Hinzufügen">
  12.     </form>
  13.     <br>
  14.     <?php
  15.       // Laden der Liste aus der Datei
  16.       $list = array();
  17.       $file = "list.txt";
  18.       if (file_exists($file)) {
  19.         $list = unserialize(file_get_contents($file));
  20.       }
  21.       if (isset($_POST['item'])) {
  22.         if (isset($_POST['add'])) {
  23.           array_push($list, $_POST['item']);
  24.         }
  25.         if (isset($_POST['edit'])) {
  26.           $index = $_POST['index'];
  27.           $list[$index] = $_POST['item'];
  28.         }
  29.         if (isset($_POST['delete'])) {
  30.           $index = $_POST['index'];
  31.           unset($list[$index]);
  32.         }
  33.         // Speichern der Liste in der Datei
  34.         file_put_contents($file, serialize($list));
  35.       }
  36.       foreach ($list as $index => $item) {
  37.         echo $item . " ";
  38.         echo "<form action='list.php' method='post'>";
  39.         echo "<input type='hidden' name='index' value='$index'>";
  40.         echo "<input type='text' name='item' value='$item'>";
  41.         echo "<input type='submit' name='edit' value='Bearbeiten'>";
  42.         echo "<input type='submit' name='delete' value='Löschen'>";
  43.         echo "</form>";
  44.         echo "<br>";
  45.       }
  46.     ?>
  47.   </body>
  48. </html>
  49.  
  50.  
File Description
  • Liste
  • PHP Code
  • 22 Jan-2023
  • 1.49 Kb
You can Share it: