Advertisement
michaelyuen

Untitled

Mar 3rd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.67 KB | None | 0 0
  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. session_start();
  10. if (!empty($_POST['product_id'], $_POST['qty'])) {
  11.     $product_id = $_POST['product_id']; // please validate input
  12.     $qty = $_POST['qty']; // please validate input
  13.     $_SESSION['cart'][$product_id] = $qty;
  14.     exit(header('location: ' . $_SERVER["HTTP_REFERER"]));
  15. }
  16.  
  17. <!-- viewcart.php -->
  18. <?php
  19. session_start();
  20. foreach ($_SESSION['cart'] as $product_id => $qty) {
  21.     echo 'Product Id: ' . $product_id . ' & Quantity: ' . $qty;
  22. }
  23. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement