Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. <!DOCTYTE html>
  2. <html lang='pl'>
  3. <head>
  4. <meta charset='utf-8' />
  5. <meta name='author' content='Patrix' />
  6.  
  7. <title>Pliki - spr</title>
  8. <meta name='description' content='Testowy skrypt obsługi plików w PHP' />
  9. </head>
  10.  
  11. <body>
  12. <form action='index.php' method='POST'>
  13. <p>
  14. Id psa:
  15. <input type='text' name='id' />
  16. </p>
  17.  
  18. <p>
  19. Imię psa:
  20. <input type='text' name='name' />
  21. </p>
  22.  
  23. <p>
  24. Wiek psa:
  25. <input type='text' name='age' />
  26. </p>
  27.  
  28. <p>
  29. Imię właściciela:
  30. <input type='text' name='owner' />
  31. </p>
  32.  
  33. <button type='reset'>Nowy</button>
  34. <button type='submit' name='button' value='1'>Zapisz</button>
  35. <button type='submit' name='button' value='2'>Dopisz</button>
  36. <button type='submit' name='button' value='3'>Wypisz</button>
  37.  
  38. <!--
  39. W powyższym przykładzie, widać, że przycisk ma name, to znaczy, że
  40. zostanie on również wysłany do tablicy $_POST, dopisałem value, by
  41. można było dane przyciski od siebie odróżnić ;). Można w value wpisać
  42. tekst, ale wysyłanie stringów do serwera jest mniej wydajne, dlatego wpisałem
  43. liczbę, możesz sobie to pomodyfikować i potestować.
  44. -->
  45. </form>
  46.  
  47. <hr />
  48.  
  49. <?php
  50.  
  51. if ($_POST)
  52. {
  53. if (isset($_POST["button"]))
  54. {
  55. $button = $_POST["button"];
  56.  
  57. switch ($button)
  58. {
  59. case 1: // zapisz
  60. $file = fopen("PLIK.txt","w");
  61.  
  62. fwrite($file, $_POST["id"]." ".$_POST["name"]." ".$_POST["age"]." ".$_POST["owner"].PHP_EOL);
  63.  
  64. fclose($file);
  65.  
  66. break;
  67.  
  68. case 2: // dopisz
  69. $file = fopen("PLIK.txt","a");
  70.  
  71. fwrite($file, $_POST["id"]." ".$_POST["name"]." ".$_POST["age"]." ".$_POST["owner"].PHP_EOL);
  72.  
  73. fclose($file);
  74.  
  75. break;
  76.  
  77. case 3: // wypisz
  78.  
  79. $file = fopen("PLIK.txt","r");
  80.  
  81. while ($line = fgets($file))
  82. {
  83. echo("<textarea disabled='disabled' style='resize: none; background: rgb(255,255,255); color: black;'>".$line."</textarea><br /><br />");
  84. }
  85.  
  86. fclose($file);
  87.  
  88. break;
  89. }
  90. }
  91. }
  92.  
  93. ?>
  94.  
  95. </body>
  96. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement