Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYTE html>
- <html lang='pl'>
- <head>
- <meta charset='utf-8' />
- <meta name='author' content='Patrix' />
- <title>Pliki - spr</title>
- <meta name='description' content='Testowy skrypt obsługi plików w PHP' />
- </head>
- <body>
- <form action='index.php' method='POST'>
- <p>
- Id psa:
- <input type='text' name='id' />
- </p>
- <p>
- Imię psa:
- <input type='text' name='name' />
- </p>
- <p>
- Wiek psa:
- <input type='text' name='age' />
- </p>
- <p>
- Imię właściciela:
- <input type='text' name='owner' />
- </p>
- <button type='reset'>Nowy</button>
- <button type='submit' name='button' value='1'>Zapisz</button>
- <button type='submit' name='button' value='2'>Dopisz</button>
- <button type='submit' name='button' value='3'>Wypisz</button>
- <!--
- W powyższym przykładzie, widać, że przycisk ma name, to znaczy, że
- zostanie on również wysłany do tablicy $_POST, dopisałem value, by
- można było dane przyciski od siebie odróżnić ;). Można w value wpisać
- tekst, ale wysyłanie stringów do serwera jest mniej wydajne, dlatego wpisałem
- liczbę, możesz sobie to pomodyfikować i potestować.
- -->
- </form>
- <hr />
- <?php
- if ($_POST)
- {
- if (isset($_POST["button"]))
- {
- $button = $_POST["button"];
- switch ($button)
- {
- case 1: // zapisz
- $file = fopen("PLIK.txt","w");
- fwrite($file, $_POST["id"]." ".$_POST["name"]." ".$_POST["age"]." ".$_POST["owner"].PHP_EOL);
- fclose($file);
- break;
- case 2: // dopisz
- $file = fopen("PLIK.txt","a");
- fwrite($file, $_POST["id"]." ".$_POST["name"]." ".$_POST["age"]." ".$_POST["owner"].PHP_EOL);
- fclose($file);
- break;
- case 3: // wypisz
- $file = fopen("PLIK.txt","r");
- while ($line = fgets($file))
- {
- echo("<textarea disabled='disabled' style='resize: none; background: rgb(255,255,255); color: black;'>".$line."</textarea><br /><br />");
- }
- fclose($file);
- break;
- }
- }
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement