Advertisement
UniQuet0p1

Untitled

Oct 28th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. <?php
  2. $error = "";
  3. function addEmployee($id,$firstName, $lastName, $grade){
  4. //ищит следубщее ид
  5. $maxId = 0;
  6. $lines = file("authors.txt");
  7. foreach ($lines as $line){
  8. $employee = explode(";", $line);
  9. if(intval($employee[0]) > $maxId){
  10. $maxId = intval($employee[0]);
  11. }
  12. }
  13.  
  14. //пишет файл
  15. $newEmployee =($maxId + 1). urlencode($id) . ";" . urlencode($firstName) . ";" . urlencode($lastName) . ";" . urlencode($grade) . PHP_EOL;
  16. file_put_contents("authors.txt", $newEmployee, FILE_APPEND);
  17.  
  18. }
  19.  
  20. if ($_SERVER["REQUEST_METHOD"] === "POST"){
  21. $id = $_POST["id"];
  22. $firstName = $_POST["firstName"];
  23. $lastName = $_POST["lastName"];
  24. $grade = $_POST["grade"];
  25. //сохранение
  26.  
  27. if ((1 > strlen($firstName) || 22 < strlen($firstName) || 2 > strlen($lastName) || 22 < strlen($lastName))){
  28. $error = "invalid data";
  29. }
  30. if (1<= strlen($firstName) && strlen($firstName) < 22 && 2 <= strlen($lastName) && strlen($lastName) < 23){
  31. $error = "";
  32. addEmployee($id, $firstName, $lastName, $grade);
  33. header("Location: author-list.php");
  34. }
  35. }
  36. ?>
  37.  
  38. <!doctype html>
  39. <html lang="en">
  40. <head>
  41. <meta charset="utf-8">
  42. <link rel="stylesheet" type="text/css" href="styles.css">
  43. <title>LisaAuthor</title>
  44. </head>
  45. <body id="author-form-page">
  46.  
  47. <div id="root">
  48.  
  49. <nav>
  50. <div>
  51. <a href="../index.php" id="book-list-link">Raamatud</a>
  52. <a href="book-add.php" id="book-form-link">Lisa raamat</a>
  53. <a href="author-list.php" id="author-list-link">Autorid</a>
  54. <a href="author-add.php" id="author-form-link">Lisa autor</a>
  55. </div>
  56.  
  57. </nav>
  58.  
  59. <main>
  60.  
  61.  
  62. <form id="input-form" method="post">
  63.  
  64. <input name="id" type="hidden" value="">
  65.  
  66. <div class="label">Eesnimi:</div>
  67. <div class="input"><input id="firstName" name="firstName" value="" type="text"/></div>
  68.  
  69. <div class="label">Perkonnanimi:</div>
  70. <div class="input"><input id="lastName" name="lastName" value="" type="text"/></div>
  71.  
  72. <div class="label">Hinne:</div>
  73. <div class="input">
  74.  
  75. <input type="radio" name="grade" value="1">1
  76. <input type="radio" name="grade" value="2">2
  77. <input type="radio" name="grade" value="3">3
  78. <input type="radio" name="grade" value="4">4
  79. <input type="radio" name="grade" value="5">5
  80. </div>
  81.  
  82. <div class="br"></div>
  83.  
  84. <div class="label"></div>
  85. <div class="input button2">
  86. <input name="submitButton" type="submit" value="Salvesta">
  87. </div>
  88.  
  89. </form>
  90. <ul id="error-block" align="center">
  91. <?php echo $error?>
  92. </ul>
  93.  
  94.  
  95. </main>
  96.  
  97. <footer>
  98. ICD0007 Näidisrakendus
  99.  
  100. </footer>
  101.  
  102. </div>
  103.  
  104. </body>
  105. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement