shitchell

Confirmation Page

Apr 1st, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.33 KB | None | 0 0
  1. <?php session_start() ?>
  2.  
  3. <?php
  4.     function validate_input($input)
  5.     {
  6.         $input = trim($input);
  7.         $input = htmlspecialchars($input);
  8.         return $input;
  9.     }
  10.  
  11.     function get_form_input($name)
  12.     {
  13.         return validate_input($_REQUEST[$name]);
  14.     }    
  15. ?>
  16.  
  17. <?php
  18.     if (get_form_input("submit-purchase"))
  19.     {
  20.         $buyPC = 'buy-pc';
  21.         $buyIphone = 'buy-iphone';
  22.         $buyMac = 'buy-mac';
  23.         $_SESSION['total'] = 0;
  24.         $_SESSION['purchasedItems'] = array();
  25.  
  26.         // Get the purchased items
  27.         if (get_form_input($buyPC) === $buyPC)
  28.         {
  29.             $_SESSION[$buyPC] = $buyPC;
  30.             $item = array();
  31.             $item['image'] = get_form_input("pc-image");
  32.             $item['price'] = get_form_input("pc-price");
  33.             $item['quantity'] = get_form_input("buy-pc-quantity");
  34.             $item['total'] = get_form_input("pc-price") * get_form_input("buy-pc-quantity");
  35.             $_SESSION['total'] += $item['total'];
  36.             array_push($_SESSION['purchasedItems'], "PC");
  37.         }
  38.         if (get_form_input($buyIphone) === $buyIphone)
  39.         {
  40.             $_SESSION[$buyIphone] = $buyIphone;
  41.             $item = array();
  42.             $item['image'] = get_form_input("iphone-image");
  43.             $item['price'] = get_form_input("iphone-price");
  44.             $item['quantity'] = get_form_input("buy-iphone-quantity");
  45.             $item['total'] = get_form_input("iphone-price") * get_form_input("buy-iphone-quantity");
  46.             $_SESSION['total'] += $item['total'];
  47.             array_push($_SESSION['purchasedItems'], "iPhone");
  48.         }
  49.         if (get_form_input($buyMac) === $buyMac)
  50.         {
  51.             $_SESSION[$buyMac] = $buyMac;
  52.             $item = array();
  53.             $item['image'] = get_form_input("mac-image");
  54.             $item['price'] = get_form_input("mac-price");
  55.             $item['quantity'] = get_form_input("buy-mac-quantity");
  56.             $item['total'] = get_form_input("mac-price") * get_form_input("buy-mac-quantity");
  57.             $_SESSION['total'] += $item['total'];
  58.             array_push($_SESSION['purchasedItems'], "iMac");
  59.         }
  60.     } else {
  61.         echo '<pre>';
  62.         print_r($_REQUEST);
  63.         echo '</pre>';
  64.         // header("Location: 04-01.php");
  65.     }
  66. ?>
  67.  
  68. <html>
  69. <head>
  70.     <style>
  71.         html {
  72.             background-color: #e3e6e8;
  73.             width: 100%;
  74.             font-family: arial, sans-serif;
  75.         }
  76.        
  77.         body {
  78.             background-color: #FFF;
  79.             width: 75%;
  80.             margin: auto;
  81.             padding: 1em;
  82.             display: flex;
  83.             justify-content: center;
  84.             box-shadow: 0 0 1em rgba(0, 0, 0, 0.5);
  85.         }
  86.        
  87.         table {
  88.             border-collapse: collapse;
  89.         }
  90.        
  91.         td {
  92.             padding: 1em;
  93.         }
  94.        
  95.         tr {
  96.             border-bottom: 1px solid #666;
  97.         }
  98.        
  99.         .product-icon {
  100.             max-width: 100%;
  101.             max-height: 100%;
  102.         }
  103.        
  104.         .center {
  105.             text-align: center;
  106.         }
  107.        
  108.         .bold {
  109.             font-weight: bold;
  110.         }
  111.     </style>
  112. </head>
  113. <body>
  114.     <form method="post" action="04-01-02.php">
  115.         <table>
  116.             <tr>
  117.                 <th>Item</th><th>Price</th><th>Quantity</th><th>Total</th>
  118.             </tr>
  119.  
  120.             <?php foreach($_SESSION['purchasedItems'] as $index => $item) : ?>
  121.  
  122.             <tr>
  123.                 <td>
  124.                     <img class="product-icon" src="<?= $item['image'] ?>" />
  125.                 </td>
  126.                 <td>
  127.                     <span class="product-price">$<?= $item['price'] ?></span>
  128.                 </td>
  129.                 <td>
  130.                     <span class="product-quantity"><?= $item['quantity'] ?></span>
  131.                 </td>
  132.                 <td>
  133.                     <span class="product-total bold">$<?= $item['total'] ?></span>
  134.                 </td>
  135.             </tr>
  136.  
  137.             <?php endforeach ?>
  138.  
  139.             <tr>
  140.                 <td colspan="3"></td>
  141.                 <td><span class="grand-total bold">$<?= $_SESSION['total'] ?></span></td>
  142.             </tr>
  143.  
  144.             <tr>
  145.                 <td colspan="4" class="center"><input type="submit" name="confirm-purchase" value="Confirm Purchase" /></td>
  146.             </tr>
  147.         </table>
  148.     </form>
  149.  
  150. </body>
  151. </html>
Add Comment
Please, Sign In to add comment