Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- html -->
- <form action="cart.php" method="POST">
- <input type="hidden" name="product_id" value="<?=product_id">
- <input type="number" name="qty">
- <button type="submit">Add To Cart</button>
- </form>
- <!-- cart.php -->
- session_start();
- if (!empty($_POST['product_id'], $_POST['qty'])) {
- $product_id = $_POST['product_id']; // please validate input
- $qty = $_POST['qty']; // please validate input
- $_SESSION['cart'][$product_id] = $qty;
- exit(header('location: ' . $_SERVER["HTTP_REFERER"]));
- }
- <!-- viewcart.php -->
- <?php
- session_start();
- foreach ($_SESSION['cart'] as $product_id => $qty) {
- echo 'Product Id: ' . $product_id . ' & Quantity: ' . $qty;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement