Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php session_start() ?>
- <?php
- function validate_input($input)
- {
- $input = trim($input);
- $input = htmlspecialchars($input);
- return $input;
- }
- function get_form_input($name)
- {
- return validate_input($_REQUEST[$name]);
- }
- ?>
- <?php
- if (get_form_input("submit-purchase"))
- {
- $buyPC = 'buy-pc';
- $buyIphone = 'buy-iphone';
- $buyMac = 'buy-mac';
- $_SESSION['total'] = 0;
- $_SESSION['purchasedItems'] = array();
- // Get the purchased items
- if (get_form_input($buyPC) === $buyPC)
- {
- $_SESSION[$buyPC] = $buyPC;
- $item = array();
- $item['image'] = get_form_input("pc-image");
- $item['price'] = get_form_input("pc-price");
- $item['quantity'] = get_form_input("buy-pc-quantity");
- $item['total'] = get_form_input("pc-price") * get_form_input("buy-pc-quantity");
- $_SESSION['total'] += $item['total'];
- array_push($_SESSION['purchasedItems'], "PC");
- }
- if (get_form_input($buyIphone) === $buyIphone)
- {
- $_SESSION[$buyIphone] = $buyIphone;
- $item = array();
- $item['image'] = get_form_input("iphone-image");
- $item['price'] = get_form_input("iphone-price");
- $item['quantity'] = get_form_input("buy-iphone-quantity");
- $item['total'] = get_form_input("iphone-price") * get_form_input("buy-iphone-quantity");
- $_SESSION['total'] += $item['total'];
- array_push($_SESSION['purchasedItems'], "iPhone");
- }
- if (get_form_input($buyMac) === $buyMac)
- {
- $_SESSION[$buyMac] = $buyMac;
- $item = array();
- $item['image'] = get_form_input("mac-image");
- $item['price'] = get_form_input("mac-price");
- $item['quantity'] = get_form_input("buy-mac-quantity");
- $item['total'] = get_form_input("mac-price") * get_form_input("buy-mac-quantity");
- $_SESSION['total'] += $item['total'];
- array_push($_SESSION['purchasedItems'], "iMac");
- }
- } else {
- echo '<pre>';
- print_r($_REQUEST);
- echo '</pre>';
- // header("Location: 04-01.php");
- }
- ?>
- <html>
- <head>
- <style>
- html {
- background-color: #e3e6e8;
- width: 100%;
- font-family: arial, sans-serif;
- }
- body {
- background-color: #FFF;
- width: 75%;
- margin: auto;
- padding: 1em;
- display: flex;
- justify-content: center;
- box-shadow: 0 0 1em rgba(0, 0, 0, 0.5);
- }
- table {
- border-collapse: collapse;
- }
- td {
- padding: 1em;
- }
- tr {
- border-bottom: 1px solid #666;
- }
- .product-icon {
- max-width: 100%;
- max-height: 100%;
- }
- .center {
- text-align: center;
- }
- .bold {
- font-weight: bold;
- }
- </style>
- </head>
- <body>
- <form method="post" action="04-01-02.php">
- <table>
- <tr>
- <th>Item</th><th>Price</th><th>Quantity</th><th>Total</th>
- </tr>
- <?php foreach($_SESSION['purchasedItems'] as $index => $item) : ?>
- <tr>
- <td>
- <img class="product-icon" src="<?= $item['image'] ?>" />
- </td>
- <td>
- <span class="product-price">$<?= $item['price'] ?></span>
- </td>
- <td>
- <span class="product-quantity"><?= $item['quantity'] ?></span>
- </td>
- <td>
- <span class="product-total bold">$<?= $item['total'] ?></span>
- </td>
- </tr>
- <?php endforeach ?>
- <tr>
- <td colspan="3"></td>
- <td><span class="grand-total bold">$<?= $_SESSION['total'] ?></span></td>
- </tr>
- <tr>
- <td colspan="4" class="center"><input type="submit" name="confirm-purchase" value="Confirm Purchase" /></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
Add Comment
Please, Sign In to add comment