Reputation: 407
I am trying to do a project that is a shopping cart. I thought based on the way I set up my forms for the items in the product list that only the ones that had been selected would show up in the cart but instead it shows all three items no matter which one I click. Can someone tell me what I need to change.
the main page where the products get listed:
<?php
session_start();
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP Final Project</title>
</head>
<body>
<?php
$_SESSION['maxItems'] = 2;
?>
<form id="item01" method="post" action="cart.php">
<?php $_SESSION['cart'][0] = array('name'=>'item01','price'=>'$11'); ?>
<label>Item 01 for sale</label>
<input type="submit" value="Add to cart" name="item01">
</form>
<br>
<form id="item02" method="post" action="cart.php">
<?php $_SESSION['cart'][1] = array('name'=>'item02','price'=>'$22'); ?>
<label>Item 02 for sale</label>
<input type="submit" value="Add to cart" name="item02">
</form>
<br>
<form id="item03" method="post" action="cart.php">
<?php $_SESSION['cart'][2] = array('name'=>'item03','price'=>'$33'); ?>
<label>Item 03 for sale</label>
<input type="submit" value="Add to cart" name="item03">
</form>
</body>
</html>
the php code for my cart page:
<?php
session_start();
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
$max_items = $_SESSION['maxItems'];
for($i=0; $i <= $max_items; $i++){
$current_name = $_SESSION['cart'][$i]['name'];
$current_price = $_SESSION['cart'][$i]['price'];
echo "item is " . $current_name . " " . $current_price . "<br>";
}
?>
</body>
</html>
Upvotes: 1
Views: 568
Reputation: 7948
First, actually, you're not getting the values of which button you clicked, you're just simply setting them as is. You need to processes the form, then, you set them on the session. Second, you really don't need multiple form tags, you only need one. Third, make some initializations, (item list, the session itself, etc.). Just create an array of items. Rough and gun example: (Same Page Form processing)
<?php
// initializations
session_start();
$_SESSION['maxItems'] = 2;
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
$item_list = array('item01' => 11, 'item02' => 22, 'item03' => 33);
// add
if(isset($_POST['add'])) {
$item = $_POST['add'];
$value = $item_list[$item];
if(count($_SESSION['cart']) >= $_SESSION['maxItems']) {
echo 'Your cart is full, sorry.';
} else {
$_SESSION['cart'][] = array('name' => $item, 'price' => $item_list[$item]);
}
}
// clear all
if(isset($_POST['remove_all'])) { $_SESSION['cart'] = array(); header('Location: '.$_SERVER['PHP_SELF']); }
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>PHP Final Project</title>
</head>
<body>
<form method="POST" action="">
<label>Item 01 for sale</label>
<button type="submit" name="add" value="item01">Add To Cart</button>
<br/>
<label>Item 02 for sale</label>
<button type="submit" name="add"value="item02">Add To Cart</button>
<br/>
<label>Item 03 for sale</label>
<button type="submit" name="add" value="item03">Add To Cart</button>
<br/><br/>
<input type="submit" name="remove_all" value="Clear Cart" />
</form>
<?php if(isset($_SESSION['cart']) && count($_SESSION['cart']) > 0): ?>
<div class="chosen_items">
<h3>Current Items</h3>
<?php foreach($_SESSION['cart'] as $key => $value): ?>
<p><?php echo "Item: <strong>". $value['name']. "</strong> Price: $".$value['price']; ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
</body>
</html>
Upvotes: 1