Sorry this has been bugging me for a while now and I still don't understand how to fix this. someone told me
NB: You don't appear to be storing the quantity anywhere on the cart. If the user updates the quantity of "pants" to 2, and then updates the quantity of "shoes" to 2, the quantity of "pants" will be reset to 1.
and now I'm just very confused because I thought that you could get all the information from a form through a post request. I have been trying to find out how to fix this but I can't get any further. so I wanna ask it like this
how do I store the quantity from my users when they submit a form. and how will the form remember the quantity
now situation:
only updates the last option so if you have a cart like this:
item quantity price
pants 1 5.00
hat 1 4.00
shoes 1 40.00
----------------------
TOTAL 49.00
and you want to have 2 pants and 2 hats then it will do this
item quantity price
pants 1 5.00
hat 1 8.00
shoes 1 40.00
----------------------
TOTAL 53.00
a. it won't remember the quantity the user put in and just set them to one and it won't remember the quantity field and just automatically set that to one. that's where I have to store quantity so that it will remember that I guess but how do i do this??
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errrors', '1');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>Cart</title>
<!-- Favicon-->
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<!-- Bootstrap icons-->
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css" rel="stylesheet" />
<!-- Core theme CSS (includes Bootstrap)-->
<link href="css/styles.css" rel="stylesheet" />
<link href="css/stylecart.css" rel="stylesheet" />
<script src="js/scripts.js" async></script>
</head>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
}
a{
text-decoration: none;
color: white;
}
</style>
<body>
<!--navbar-->
<a class="back" href="index.php"> class="bi bi-arrow-left-circle-fill bi-5x"></a>
<?php
include "config.php";
?>
<div class="text-center" style="font-size: 100px;">🛍</div>
<h2 class="text-center">Winkelmandje</h2><br>
<section class="container content-section">
<!-- <h2 class="section-header">CART</h2> -->
<div class="cart-row">
ITEM
PRICE
QUANTITY
berekening
<!-- Verwijderen -->
</div>
<?php
$broodjes = $_GET['broodjes_ID'];
if (isset($_SESSION['basket'])){
if( in_array( $broodjes ,$_SESSION['basket']) )
{
}else{
$_SESSION['basket'][] = $broodjes;
}
}else{
$_SESSION['basket'][]= $broodjes;
}
$sumtotal = 0;
foreach($_SESSION['basket'] as $key => $value){
$_SESSION['quantity'] = array();
$sql = "SELECT broodjes_ID, broodnaam, prijs, voorraad FROM broodjes WHERE broodjes_ID=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $value);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()){
echo '<div class="cart-items">';
echo '<div class="cart-row">';
echo '<div class="cart-item cart-column">';
echo $row['broodnaam'];
echo '</div>';
echo '<div class="cart-item cart-column">';
echo '€ ' . $row['prijs'];
echo '</div>';
echo '<div class="cart-item cart-column">';
echo '<form method="POST" action"">';
echo '<div class="col-xs-4">';
echo '<input type="hidden" name="broodnaam" id="broodnaam" value="' . $row['broodnaam'] . '">';
echo '<input type="number" name="quantity" id="quantity" class="form-control input-sm" placeholder="1" min="1" max="100" value="1">';
echo '</div>';
echo '</form>';
echo '</div>';
array_push($_SESSION['quantity'], $_POST['quantity']);
print_r($_SESSION['quantity']);
$quantity = 1;
if (isset($_POST['quantity']) && !empty($_POST['quantity'])){
$_SESSION['quantity'] = $_POST['quantity'];
if (isset($_POST['broodnaam']) && !empty($_POST['broodnaam'])){
if ($_POST['broodnaam'] == $row['broodnaam']){
$quantity = $_POST['quantity'];
}
}
}
echo '<div class="cart-item cart-column">';
$rowtotaal = $row['prijs'] * $quantity;
$sumtotal += $rowtotaal;
echo $rowtotaal;
echo '</div>';
echo '</div>';
echo '</div>';
}
}
?> <br />
<div class="cart-total">
class="cart-total-title">Total
€ <?php echo $sumtotal;?>
</div>
<br/>
<button type="button" class="btn btn-danger"><a href="bestellen.php"> Bestellen</a></button>
<br>
<br />
</section>
</body>
What I have tried:
I have been trying to look up that I have to do but all I see is just use $_POST['quantity']; so idk what I'm doing wrong