简单的购物车

有两个文件就能实现

<html>
<head>
<title>购物车</title>
<LINK href="css/reg.css" type=text/css rel=stylesheet>
</head>
<body>
<form action="myCart.php" method="post">
<div class="mainbox formbox">
<h1>购物车</h1>
<table  cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>商品</th>
<td> </td>
</tr>
</thead>
<tr>
<th><input type="checkbox" name="item[]" value="6"></th>
<th>铅笔</th>
</tr> 
<tr>
<th><input type="checkbox" name="item[]" value="1"></th>
<th>钢笔</th>
</tr>
<tr>
<th><input type="checkbox" name="item[]" value="2"></th>
<th>毛笔</th>
</tr>
<tr>
<th><input type="checkbox" name="item[]" value="3"></th>
<th>画笔</th>
</tr>
<tr>
<th><input type="checkbox" name="item[]" value="4"></th>
<th>金笔</th>
</tr>
</table>
<table summary="Submit Button" cellpadding="0" cellspacing="0">
<tr>
<th> </th>
<td>
<button class="submit" type="submit" name="submit" value="asdfas">放入购物车</button>
</td>
</table>
</div>
</form>
</body>
</html>

下面是第二个页面

<?php
session_start();
$items=array("照相机","手机","笔记本电脑","羽绒服","皮鞋");//定义一个存储商品的数组
$itemCount=$_SESSION["itemCount"];//从会话中获得商品数量
if ($itemCount==null){
$itemCount=0;
}
$itemsSelected=$_POST['item'];// 取得表单上选中的商品
if (isset($itemsSelected)){// 判断是否选中商品
for ($i=0;$i<count($itemsSelected);$i++){
$itemCount=$itemCount+1;
$_SESSION["itemCount"]=$itemCount;// 将商品数量放入会话中
$_SESSION["itemList"][$itemCount]=$itemsSelected[$i];//将选中的商品放入会话中
}
}
/*
$_SESSION["itemList"][0] = 0
$_SESSION["itemList"][1] = 1
$_SESSION["itemList"][2] =2 
$_SESSION["itemList"][3] 3
*/
?>
<html>
<head>
<title>购物车</title>
<LINK href="css/css.css" type=text/css rel=stylesheet>
</head>
<body>
<div class="mainbox threadlist">
 <div class="headactions">
</div>
<h1>购物车</h1>
 <table class="portalbox"  cellpadding="0" cellspacing="1" >
 <thead class="separation">
<tr>
<td>商品名称</td>
</tr>
</thead>
</div>
<?php
for ($i=0;$i<$itemCount;$i++){
$item=$_SESSION["itemList"][$i+1];  
?>
<tr>
<td><?php echo $items[$item] ;// 输出购物车中的商品 ?></td>
</tr>
<?php
}
?>
<tr>
<td>您购物车中共有<?php echo $itemCount;?>件商品</td>
</tr>showItemInfo.php
</table>
<center><a href="showItemInfo.php">继续购物</a></center>
</body>
</html>

复制下代码分别命名为showItemInfo.php和myCart.php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值