Advertisement
michaelyuen

Untitled

Mar 3rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!-- html -->
  2. <form action="cart.php" method="POST">
  3.     <input type="hidden" name="product_id" value="<?=product_id">
  4.     <input type="number" name="qty">
  5.     <button type="submit">Add To Cart</button>
  6. </form>
  7.  
  8. <!-- cart.php -->
  9. <?php
  10. session_start();
  11. if (!empty($_POST['product_id'], $_POST['qty'])) {
  12.     $product_id = $_POST['product_id']; // please validate input
  13.     $qty = $_POST['qty']; // please validate input
  14.     $_SESSION['cart'][$product_id] = $qty;
  15.     exit(header('location: ' . $_SERVER["HTTP_REFERER"]));
  16. }
  17. ?>
  18.  
  19. <!-- viewcart.php -->
  20. <?php
  21. session_start();
  22. foreach ($_SESSION['cart'] as $product_id => $qty) {
  23.     echo 'Product Id: ' . $product_id . ' & Quantity: ' . $qty;
  24. }
  25. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement