商城项目-购物车内的东西一键购买

一键清空购物车

  最近自己在做一个购物网站的小项目,在做到购物车下单的时候遇到了一个小问题,想用复选框来选择物品,选择后一键快速购买,自己写代码琢磨了一下,其实也很简单,做好了之后写篇随笔记录下来,也希望能帮到其他人.

  用 foreach 函数遍历复选框选择的数组.

贴代码:

speed_buy.php

 1 <!DOCTYPE html>
 2 <!--
 3 实现功能:一键快速购买
 4 -->
 5 <html>
 6 <head>
 7     <meta charset="utf-8"/>
 8     <title>快速购买购物车内的东西</title>
 9 </head>
10 <style>
11     .body {
12          100%;
13         height: auto;
14         clear: both;
15         background-color: #7a848d;
16     }
17     .car {
18         float: right;
19          90%;
20         height: auto;
21         box-sizing: border-box;
22         background-color: #0affeb;
23         margin-top: 10px;
24         text-align: center;
25     }
26     .buy {
27          50%;
28         height: 30px;
29         border: none;
30         background-color: #7f7f7f;
31         color: white;
32     }
33     .check {
34         float: left;
35         background-color: #00a3f0;
36          10%;
37         height: 30px;
38         margin-top: 20px;
39     }
40     .input_buy {
41          100%;
42         height: 40px;
43         border: none;
44         background-color: #00a3f0;
45         color: white;
46     }
47 </style>
48 <body>
49 <?php
50 $id = 0;
51 //for循环是为了模仿从数据库拿出不同的商品ID
52 for ($i = 0;$i <= 10;$i++) {
53 $id += 1;
54 ?>
55 <form action="./buy.php" method="post" accept-charset="utf-8">
56     <div class="body">
57         <div class="check">
58             <!--将name赋值为一个数组,将商品ID赋值给value-->
59             <input type="checkbox" name="car[]" value="<?= $id ?>">
60         </div>
61         <div class="car">
62             <a>此商品ID为:<?= $id ?></a><br>
63             <input class="buy" type="submit" value="购买">
64         </div>
65     </div>
66     <?php
67     }
68     ?>
69     <div><input class="input_buy" type="submit" value="一键购买"></div>
70 </form>
71 </body>
72 </html>

  上面的代码实现了购物车的界面

buy.php

 1 <?php
 2 header('Content-Type:text/html;charset=utf-8');
 3 ?>
 4 <!DOCTYPE html>
 5 <html>
 6 <head>
 7     <meta charset="utf-8"/>
 8     <title>一键购买</title>
 9 </head>
10 <style>
11     .buy{
12          100px;
13         height: 100px;
14         text-align: center;
15         background-color: black;
16         color: white;
17         margin: 20px;
18     }
19 </style>
20 <body>
21 <?php
22 $buy=$_REQUEST['car'];
23 //关键位置
24 foreach ($buy as $v) {//遍历数组简洁版语法
25 //把下单的php方法写在foreach里面就可以实现快速购买
26     ?>
27 <div class="buy">
28     <a><?=$v?></a>
29 </div>
30 <?php
31 }
32 ?>
33 </body>
34 </html>

配图:

speed_buy.phpbuy.php

时间:2018年8月17日21:39:32

原文地址:https://www.cnblogs.com/lygz/p/9495427.html