简体   繁体   English

PHP购物车显示未选择的项目

[英]PHP shopping cart showing unselected items

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代码:

<?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>

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>

Sample Output 样本输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM