Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.57 KB | None | 0 0
  1. <form action="index.php?page=cart" method="post">
  2. <input type="hidden" name="name" value="<?=$product['name']?>">
  3.  
  4.            <input type="number" name="quantity" value="1" min="1" max="<?=$product['quantity']?>" placeholder="Quantity" required>
  5.            <input type="hidden" name="product_id" value="<?=$product['id']?>">
  6. <select name="color">
  7. <option value="Blue">Blue</option>
  8. <option value="Red">Red</option>
  9. <option value="Yellow">Yellow</option>
  10. </select>
  11.  
  12.            <input type="submit" value="Add To Cart" name="submit">
  13.        </form>
  14.  
  15.  
  16. Pagina cu cosul de cumparaturi
  17.  
  18.  
  19. echo $color = $_POST['color'];
  20.  
  21. $tva=10.00;
  22.  
  23. if (isset($_POST['product_id'], $_POST['quantity'], $color) && is_numeric($_POST['product_id']) && is_numeric($_POST['quantity'])) {
  24.  
  25.   $product_id = (int)$_POST['product_id'];
  26.    $quantity = (int)$_POST['quantity'];
  27.  
  28.  
  29.    $stmt = $pdo->prepare('SELECT * FROM products WHERE id = ?';
  30.    $stmt->execute([$_POST['product_id']]);
  31.  
  32.    $product = $stmt->fetch(PDO::FETCH_ASSOC);
  33.  
  34.    if ($product && $quantity > 0) {
  35.  
  36.       if (isset($_SESSION['cart']) && is_array($_SESSION['cart'])) {
  37.           if (array_key_exists($product_id, $_SESSION['cart'])) {
  38.  
  39.               $_SESSION['cart'][$product_id] += $quantity;
  40.  
  41.            } else {
  42.        
  43.                $_SESSION['cart'][$product_id] = $quantity;
  44.              
  45.  
  46.            }
  47.        } else {
  48.  
  49.            $_SESSION['cart'] = array($product_id => $quantity); // aici am incercat sa mai adaug culoare gen => array('quantity' => $quantity, 'color' => $_POST['color']));
  50.  
  51.        }
  52.    }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement