Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form action="index.php?page=cart" method="post">
- <input type="hidden" name="name" value="<?=$product['name']?>">
- <input type="number" name="quantity" value="1" min="1" max="<?=$product['quantity']?>" placeholder="Quantity" required>
- <input type="hidden" name="product_id" value="<?=$product['id']?>">
- <select name="color">
- <option value="Blue">Blue</option>
- <option value="Red">Red</option>
- <option value="Yellow">Yellow</option>
- </select>
- <input type="submit" value="Add To Cart" name="submit">
- </form>
- Pagina cu cosul de cumparaturi
- echo $color = $_POST['color'];
- $tva=10.00;
- if (isset($_POST['product_id'], $_POST['quantity'], $color) && is_numeric($_POST['product_id']) && is_numeric($_POST['quantity'])) {
- $product_id = (int)$_POST['product_id'];
- $quantity = (int)$_POST['quantity'];
- $stmt = $pdo->prepare('SELECT * FROM products WHERE id = ?';
- $stmt->execute([$_POST['product_id']]);
- $product = $stmt->fetch(PDO::FETCH_ASSOC);
- if ($product && $quantity > 0) {
- if (isset($_SESSION['cart']) && is_array($_SESSION['cart'])) {
- if (array_key_exists($product_id, $_SESSION['cart'])) {
- $_SESSION['cart'][$product_id] += $quantity;
- } else {
- $_SESSION['cart'][$product_id] = $quantity;
- }
- } else {
- $_SESSION['cart'] = array($product_id => $quantity); // aici am incercat sa mai adaug culoare gen => array('quantity' => $quantity, 'color' => $_POST['color']));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement