在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
<?php session_start(); include_once("DB.class.php"); //接受并解析前端传过来的json,转换成数组。 $goods_list =$_POST['order']; $goods_list= json_decode($goods_list, true); //1.获取session信息 $arr = $_SESSION['gwc']; //获得订单数组 $item_list = $goods_list['order']; //一维实现,每次先清空订单。 $order = ''; //把商品信息存到订单,并且把他们从购物车中删除 foreach($item_list as $k=> $item){ //把商品信息存到订单 $order[$k] = $arr[$k]; //把商品信息从购物车中删除 unset($arr[$k]); } //更新购车的商品信息 if(!empty($arr)){ $_SESSION['gwc'] = $arr; }else{ $_SESSION['gwc'] = ''; } //最后将订单信息存到session $_SESSION['order'] = $order; // var_dump($_SESSION); // die(); echo "OK"; exit; ?> 注:例子中是使用的工具类代码DB.class.php要在https://www.cnblogs.com/mzzone/p/10911891.html
业务逻辑
购物车页面的结算按钮注册点击事件(需要引入jquery文件) $('#jiesuan').click( function(){ //间接设置订单金额 var j = $('.total_text').text(); // 去除¥符号 var order_toal = j.substring(1); //将标签的值赋给input传过去 $("#order_total").val(order_toal); //用插件将表单生成json,在ajax发送过去后台 var serializeObj=$('#form1').serializeJSON(); var serializeStr=JSON.stringify(serializeObj); console.log(serializeStr); $.ajax({ cache: false, type: "POST", url : "add_to_order.php", traditional:true, data: {order : serializeStr}, //注意这里的写法,键为order success: function(msg){ if(msg=="OK"){ alert("下单成功"); // location.reload(); //window.location.href='order.php'; }else{ alert(msg); alert("下单失败"); } } }); 表单中的一部分代码,展示如何把选中的商品传过去。关键在于checbox键值设置:在foreache循环中,所以键值得有变量混合进去。<!-- 1.遍历店铺,查看有多少商品并遍历。--> <div class="shop_info"> <div class="all_check"> <!--店铺全选--> <input type='checkbox' id='shop_a' class='shopChoice'> <label for='shop_a' class='shop'></label> </div> <div class="shop_name"> 店铺:<a href="javascript:;">蜜食专家</a> </div> </div> <?php session_start(); include_once("DB.class.php"); if(isset($_SESSION['gwc'])&&!empty($_SESSION['gwc'])){ $arr2 = $_SESSION['gwc']; foreach($arr2 as $k1=> $arr){ $goods_id = $arr['goods_id']; $sql="select * from tb_goods where id=$goods_id"; $goods = $dao->getRow($sql); $goods_num = $arr['num']; $sum = $goods['price']*$goods_num; ?> <div class="order_content"> <!-- 最低应该在这里开始遍历,有分割线 --> <!-- 利用上面的php变量,遍历一个商品或sku --> <ul class="order_lists"> <li class="list_chk"> <!-- 每个复选框的值存她的商品id或sku_id --> <?php echo "<input type='checkbox' id='checkbox_$goods_id' name='order[$goods_id]' value='$goods_id' class='son_check'>"; ?> <!-- 打勾对应文本框的id --> <?php echo "<label for='checkbox_$goods_id'></label>"; ?> </li> <li class="list_con"> <div class="list_img"><a href="javascript:;"> <?php echo "<img src='./images/$goods[img]' alt=''>";?> </a></div> <div class="list_text"><a href="javascript:;"><?php echo "$goods[goodsname]"; ?></a></div> </li> <li class="list_info"> <p>规格:默认</p> </li> <li class="list_price"> <p class="price"><?php echo '¥'."$goods[price]"; ?></p> <?php echo "<input type='hidden' value='$goods[price]'>"; ?> </li> <li class="list_amount"> <div class="amount_box"> <!-- 对商品的数量进行加减 --> <?php echo "<a href='change_number.php?goods_id=$goods_id&choice=jian' class='reduce reSty'>-</a> "; ?> <!-- 数量,要从session或数据库中查出来 --> <?php echo "<input type='text' value='$goods_num' class='sum'>"; ?> <?php echo "<a href='change_number.php?goods_id=$goods_id&choice=jia' class='plus'>+</a> "; ?> <?php echo "<input type='hidden' value='$goods[number]'>"; ?> </div> </li> <li class="list_sum"> <p class="sum_price"><?php echo '¥'."$sum"; ?></p> </li> <li class="list_op"> <p class="del"> <?php echo "<a href='delete_goods.php?goods_id=$goods_id' class='delBtn'>删除宝贝</a> "; ?></p> </li> </ul> </div> <?php }} else{ echo "<h1 style='margin-top:150px;text-align:center;font-size: 39px;color:#c18e2cdb;'>兄 dei,你的购物车只剩下尊严了!!!</h1>"; } ?>
注意事项 可以给商品的checkbox类型添加name属性是因为他有selected属性,而其他像hidden类型的不能给它name属性,否则也会提交到后台。要保持提交过去的数据不能有冗余,需要遍历。 而且添加到订单也只需商品id就行,订单的商品数量可以在根据key在gwc中获得。具体看add_to_order.php. |
2022-08-18
2022-07-22
2022-08-17
2022-11-06
2022-08-15
请发表评论