diff --git a/html/data_treatment/AJAX/modifier_panier.php b/html/data_treatment/AJAX/modifier_panier.php new file mode 100644 index 0000000000000000000000000000000000000000..fa188d16f60a698e3405de86591b230849e7ea95 --- /dev/null +++ b/html/data_treatment/AJAX/modifier_panier.php @@ -0,0 +1,28 @@ +<?php +session_start(); +include("../connect_database.php"); + +// Vérifier si l'article est déjà dans le panier +if (!isset($_SESSION['panier'])) { + $_SESSION['panier'] = array(); +} + +$itemName = $_POST['itemName']; +$itemPrice = $_POST['itemPrice']; +$itemQuantity = $_POST['itemQuantity']; + +// Vérifier si l'article est déjà dans le panier +if (array_key_exists($itemName, $_SESSION['panier'])) { + // L'article est déjà dans le panier, mettez à jour la quantité + $_SESSION['panier'][$itemId]['quantity'] += $itemQuantity; +} else { + // L'article n'est pas encore dans le panier, ajoutez-le au panier et à la base de données + $_SESSION['panier'][$itemName] = array( + 'price' => $itemPrice, + 'quantity' => $itemQuantity + ); +} + +$response = array('success' => true, 'message' => 'Article ajouté au panier avec succès'); +echo json_encode($response); +?> diff --git a/startbootstrap-agency-gh-pages/index.php b/startbootstrap-agency-gh-pages/index.php index 03a057097286a3272fe1953776f8182dd95b123d..a0e94d9c5f6ad8cb156b74bc18861065c94e45bc 100644 --- a/startbootstrap-agency-gh-pages/index.php +++ b/startbootstrap-agency-gh-pages/index.php @@ -303,38 +303,41 @@ echo "Hello Mr./Ms. ".explode('@',$_SESSION["username"])[0].", you were last onl <!-- Portfolio Modals--> <!-- Portfolio item 1 modal popup--> <?php - for($i=0; $i<count($articles); $i++){ + for($i=0; $i<count($articles); $i++){ ?> - <div class="portfolio-modal modal fade" <?php echo 'id="portfolioModal'.$i.'"' ?> tabindex="-1" role="dialog" aria-hidden="true"> - <div class="modal-dialog"> - <div class="modal-content"> - <div class="close-modal" data-bs-dismiss="modal"><img src="assets/img/close-icon.svg" alt="Close modal" /></div> - <div class="container"> - <div class="row justify-content-center"> - <div class="col-lg-8"> - <div class="modal-body" <?php echo 'id="modal'.$articles[$i]["itemName"].'"';?>> - <!-- Project details--> - <h2 class="text-uppercase"><?php echo $articles[$i]["itemName"]?></h2> - <!-- <p class="item-intro text-muted">Lorem ipsum dolor sit amet consectetur.</p> --> - <img class="img-fluid d-block mx-auto" src=<?php echo "'../image/laptops/".$articles[$i]["imagename"]."'"; ?> alt="..." /> - <p><?php echo $articles[$i]["description"]; ?></p> - <p><?php echo "Price: ".$articles[$i]["price"]." € <br>Quantity available: ".$articles[$i]["quantity"];?></p> - <div class="quantity"> - <button class="remove-button" onclick="removeProduct(this)">X</button> - <button onclick="decrementQuantity(this)">-</button> - <input type="text" value="1" <?php echo 'id="quantity"'.$i; ?>> - <button onclick="incrementQuantity(this)">+</button> + <div class="portfolio-modal modal fade" <?php echo 'id="portfolioModal'.$i.'"' ?> tabindex="-1" role="dialog" aria-hidden="true"> + <div class="modal-dialog"> + <div class="modal-content"> + <div class="close-modal" data-bs-dismiss="modal"><img src="assets/img/close-icon.svg" alt="Close modal" /></div> + <div class="container"> + <div class="row justify-content-center"> + <div class="col-lg-8"> + <div class="modal-body" <?php echo 'id="modal'.$articles[$i]["itemName"].'"';?>> + <!-- Project details--> + <h2 class="text-uppercase"><?php echo $articles[$i]["itemName"]?></h2> + <img class="img-fluid d-block mx-auto" src=<?php echo "'../image/laptops/".$articles[$i]["imagename"]."'"; ?> alt="..." /> + <p><?php echo $articles[$i]["description"]; ?></p> + <p><?php echo "Price: ".$articles[$i]["price"]." € <br>Quantity available: ".$articles[$i]["quantity"];?></p> + <div class="quantity"> + <button class="remove-button" onclick="removeProduct(this)">X</button> + <button onclick="decrementQuantity(this)">-</button> + <input type="text" value="1" <?php echo 'id="quantity'.$i.'"'; ?>> + <button onclick="incrementQuantity(this)">+</button> + </div> + <button class="btn btn-primary btn-xl text-uppercase" type="button" + data-item-id="<?php echo $i; ?>" + data-item-name="<?php echo $articles[$i]["itemName"]; ?>" + data-item-price="<?php echo $articles[$i]["price"]; ?>" + onclick="addToCart(this)"> + Add to shopping cart + </button> </div> - <button class="btn btn-primary btn-xl text-uppercase" type="button" <?php echo 'id = "buttonShoppingCart'.$i.'"';?>> - Add to shopping cart - </button> </div> </div> </div> </div> </div> </div> - </div> <?php }; ?> <!-- Bootstrap core JS--> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script> @@ -380,6 +383,30 @@ echo "Hello Mr./Ms. ".explode('@',$_SESSION["username"])[0].", you were last onl // Initial update updateOnlineUsers(); + function addToCart(button) { + var itemId = button.getAttribute("data-item-id"); + var itemName = button.getAttribute("data-item-name"); + var itemPrice = button.getAttribute("data-item-price"); + var itemQuantity = document.getElementById("quantity" + itemId).value; + + // Faire une requête Ajax pour ajouter l'article au panier + $.ajax({ + url: '../html/data_treatment/AJAX/modifier_panier.php', + method: 'POST', + data: { + itemName: itemName, + itemPrice: itemPrice, + itemQuantity: itemQuantity + }, + dataType: 'json', + success: function (response) { + alert(response.message); // Vous pouvez personnaliser cela en fonction de votre logique + }, + error: function (error) { + console.error(error); + } + }); + } </script> </body>