Advertisement
Guest User

shopping list

a guest
Apr 1st, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.02 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. if (!isset($_SESSION['list'])) $_SESSION['list'] = array();
  5.  
  6. $action = filter_input(INPUT_GET, "action");
  7. if ($action == "clear") {
  8.     $_SESSION['list'] = array();
  9.     header("Location: " . $_SERVER['PHP_SELF']);
  10. }
  11.  
  12. $shopItem = strip_tags(filter_input(INPUT_POST, "newItem"));
  13. if ($shopItem) array_push($_SESSION['list'], $shopItem);
  14.  
  15. ?><!DOCTYPE html>
  16. <html>
  17.     <head>
  18.         <meta charset="utf-8" />
  19.         <title>Shopping list</title>
  20.         <style>td, th {
  21.                 padding: 0.2em
  22.             }
  23.         </style>
  24.     </head>
  25.     <body>
  26.         <div id ="shopping">
  27.             <form method ="post">
  28.                 <input type="text" name ="newItem" placeholder ="New Item" autofocus />
  29.                 <input type ="submit" value="Add" />
  30.             </form>
  31.             <a href="?action=clear">Clear list</a>
  32.             <table>
  33.                 <thead>
  34.                     <tr><th>#</th><th>Item</th></tr>
  35.                 </thead>
  36.                 <tbody>
  37. <?php
  38. foreach ($_SESSION['list'] as $i => $item) echo "\t\t\t\t\t<tr><td>" . ($i + 1) . "</td><td>$item</td></tr>" . PHP_EOL;
  39. ?>
  40.                 </tbody>
  41.             </table>
  42.         </div>
  43.     </body>
  44. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement