zoukankan      html  css  js  c++  java
  • ajax、PHP、session做购物车

    购物车网页代码

    1.登录界面login.php

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="../jquery-1.11.2.min.js"></script>
    <title>无标题文档</title>
    </head>
    
    <body>
    <div>用户名:<input type="text" id="uid" /></div>
    <div>密码:<input type="text" id="pwd" /></div>
    <input type="button" value="登录" id="btn" />
    </body>
    <script type="text/javascript">
    $("#btn").click(function(){
        var uid = $("#uid").val();
        var pwd = $("#pwd").val();
        $.ajax({
                url:"loginchuli.php",
                data:{u:uid,p:pwd},
                type:"POST",
                dataType:"TEXT",
                success: function(data){
                    if(data.trim()=="OK")
                    {
                        window.location.href="main.php";
                    }
                    else
                    {
                        alert("用户名或密码错误");
                    }
                }
            })
        })
    </script>
    </html>

    2.登录处理页面loginchuli.php

     1 <?php
     2 session_start();
     3 
     4 include("../DBDA.class.php");
     5 $db = new DBDA();
     6 $uid = $_POST["u"];
     7 $pwd = $_POST["p"];
     8 $sql = "select password from login where username='{$uid}'";
     9 $mm = $db->StrQuery($sql);
    10 if($mm==$pwd && $pwd!="")
    11 {
    12     $UserName = $_POST["uid"];
    13     $_SESSION["uid"]=$uid;
    14     echo "OK";
    15 }
    16 else
    17 {
    18     echo "NO";
    19 }

    3.主页面main.php

     1 <?php
     2 session_start();
     3 include("../DBDA.class.php");
     4 $db = new DBDA();
     5 ?>
     6 
     7 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     8 <html xmlns="http://www.w3.org/1999/xhtml">
     9 <head>
    10 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    11 <title>无标题文档</title><br />
    12 <style type="text/css">
    13 .list{ 100%; height:30px; margin-top:10px; text-align:center; line-height:30px; vertical-align:middle}
    14 </style>
    15 </head>
    16 <body>
    17 <div style="100%; height:100px; background-color:#6CC">
    18     <h1 style="float:left">大苹果商城</h1>
    19     <a style="float:right; margin-top:40px" href="zhuxiao.php">注销</a>
    20 </div>
    21 <br />
    22 <div style="100%; height:600px">
    23     <div id="left" style="20%; float:left">
    24         <a href="main.php"><div class="list">浏览商品</div></a>
    25         <a href="zhanghu.php"><div class="list">查看账户</div></a>
    26         <a href="gouwuche.php"><div class="list">查看购物车</div></a>
    27     </div>
    28     
    29     <div id="right" style="80%; float:left">
    30 
    31 <?php
    32     $agwc = array();
    33     if(!empty($_SESSION["gwc"]))
    34     {
    35         $agwc = $_SESSION["gwc"];
    36     }
    37     $zhonglei = count($agwc);
    38     $sum = 0;
    39     foreach($agwc as $v)
    40     {
    41         $sql = "select price from fruit where ids='{$v[0]}'";
    42         $danjia = $db->StrQuery($sql);
    43         $sum = $sum +$danjia*$v[1];
    44     }
    45     echo "<div>购物车中有:{$zhonglei}种商品,总价格为:{$sum}元.</div>";
    46     ?>
    47 
    48         <table width="100%" border="1" cellpadding="0" cellspacing="0">
    49         <tr>
    50             <td>代号</td>
    51             <td>水果名称</td>
    52             <td>水果价格</td>
    53             <td>源产地</td>
    54             <td>库存量</td>
    55             <td>操作</td>
    56         </tr>
    57 
    58  <?php     
    59         $sql = "select * from fruit";
    60         $attr = $db->Query($sql);
    61         
    62         foreach($attr as $v)
    63         {
    64             echo "<tr><td>{$v[0]}</td>
    65             <td>{$v[1]}</td>
    66             <td>{$v[2]}</td>
    67             <td>{$v[3]}</td>
    68             <td>{$v[4]}</td>
    69             <td><a href='goumai.php?code={$v[0]}'>购买</a></td></tr>";
    70         }
    71         ?>        
    72 
    73         </table>
    74     </div>
    75 </div>
    76 
    77 </body>
    78 </html>

    4.购买处理页面goumai.php

     1 <?php
     2 session_start();
     3 $code = $_GET["code"];
     4 
     5 if(empty($_SESSION["gwc"]))
     6 {
     7     //第一次点击购买
     8     $attr = array(
     9         array($code,1)
    10     );
    11     $_SESSION["gwc"] = $attr;
    12 }
    13 else
    14 {
    15     //不是第一次点击购买
    16     $attr = $_SESSION["gwc"];
    17     $bs=0;
    18     foreach($attr as $k=>$v)
    19     {
    20         if($v[0]==$code)
    21         {
    22             $bs=1;
    23             $attr[$k][1] = $attr[$k][1]+1;
    24         }
    25     }
    26     //如果没有在数组里面出现
    27     if($bs==0)
    28     {
    29         $shuzu = array($code,1);
    30         $attr[] = $shuzu;
    31     }
    32     
    33     $_SESSION["gwc"]=$attr;
    34     
    35 }
    36 header("location:main.php");

    5.订单处理页面,计算选取水果的总价,和水果剩余量。dingdan.php

     1 <?php
     2 session_start();
     3 include("../DBDA.class.php");
     4 $db = new DBDA();
     5 $uid = $_SESSION["uid"];
     6 $attr = array();
     7 if(!empty($_SESSION["gwc"]))
     8 {
     9     $attr = $_SESSION["gwc"];
    10 }
    11 //看下两个条件是否都满足
    12 $bs = true;
    13 
    14 //判断余额是否满足
    15     //根据用户名找余额
    16     $syue = "select account from login where username='{$uid}'";
    17     $yue = $db->StrQuery($syue);
    18     
    19     //根据购物车数组取总金额
    20     $sum = 0;
    21     foreach($attr as $v)
    22     {
    23         $sql = "select price from fruit where ids='{$v[0]}'";
    24         $danjia = $db->StrQuery($sql);
    25         $sum = $sum +$danjia*$v[1];
    26     }
    27     if($yue<$sum)
    28     {
    29         $bs = false;
    30         echo "YEBUZU";
    31         exit;
    32     }
    33     
    34 //判断库存是否满足
    35 
    36 foreach($attr as $v)
    37 {
    38     $skucun = "select name,numbers from fruit where ids='{$v[0]}'";
    39     $akucun = $db->Query($skucun);
    40     if($akucun[0][1]<$v[1])
    41     {
    42         $bs = false;
    43         echo "{$akucun[0][0]}库存不足!";
    44         exit;
    45         
    46     }
    47 }
    48 
    49 //添加订单,减库存,减余额
    50 
    51 if($bs)
    52 {
    53     //减库存
    54     foreach($attr as $v)
    55     {
    56         $sql = "update fruit set numbers = numbers-{$v[1]} where ids='{$v[0]}'";
    57         $db->Query($sql,0);
    58     }
    59     
    60     //减余额
    61     $jianyue="update login set account=account-{$sum} where username='{$uid}'";
    62     $db->Query($jianyue,0);
    63     
    64     //添加订单
    65     $dingdanhao = $uid+date("YmdHis");
    66     $t = time();
    67     
    68     $sorder = "insert into orders values('{$dingdanhao}','{$uid}','{$t}')";
    69     $db->Query($sorder,0);
    70     
    71     foreach($attr as $v)
    72     {
    73         $sxq = "insert into orderdetails values('','{$dingdanhao}','{$v[0]}','{$v[1]}')";
    74         $db->Query($sxq,0);
    75     }
    76 }
    77 
    78 echo "OK";

    6.购物车页面

     1 <?php
     2 session_start();
     3 ?>
     4 
     5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     6 <html xmlns="http://www.w3.org/1999/xhtml">
     7 <head>
     8 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     9 <title>无标题文档</title><br />
    10 <style type="text/css">
    11 .list{ 100%; height:30px; margin-top:10px; text-align:center; line-height:30px; vertical-align:middle}
    12 </style>
    13 <script src="../../jquery-1.11.2.min.js"></script>
    14 </head>
    15 
    16 <body>
    17 <div style="100%; height:100px; background-color:#6CC">
    18     <h1 style="float:left">大苹果商城</h1>
    19     <a style="float:right; margin-top:40px" href="zhuxiao.php">注销</a>
    20 </div>
    21 <br />
    22 <div style="100%; height:600px">
    23     <div id="left" style="20%; float:left">
    24         <a href="main.php"><div class="list">浏览商品</div></a>
    25         <a href="zhanghu.php"><div class="list">查看账户</div></a>
    26         <a href="gouwuche.php"><div class="list">查看购物车</div></a>
    27     </div>
    28     
    29     <div id="right" style="80%; float:left">
    30         <table width="100%" border="1" cellpadding="0" cellspacing="0">
    31         <tr>
    32             <td>商品名称</td>
    33             <td>商品单价</td>
    34             <td>购买数量</td>
    35             <td>操作</td>
    36         </tr>
    37 
    38 <?php
    39         include("../DBDA.class.php");
    40         $db = new DBDA();
    41         $attr=array();
    42         if(!empty($_SESSION["gwc"]))
    43         {
    44             $attr = $_SESSION["gwc"];
    45         }
    46         
    47         foreach($attr as $k=>$v)
    48         {
    49             $sql = "select name,price from fruit where ids='{$v[0]}'";
    50             $ashuiguo = $db->Query($sql);
    51         
    52             echo "<tr><td>{$ashuiguo[0][0]}</td><td>{$ashuiguo[0][1]}</td><td>{$v[1]}</td><td><a href='shanchu.php?sy={$k}'>删除</a></td></tr>";
    53             
    54         }
    55         
    56         ?>
    57 
    58         </table>
    59         <div id="tj">提交订单</div><div id="ts"></div>
    60     </div>
    61 </div>
    62 
    63 <script type="text/javascript">
    64 $("#tj").click(function(){
    65         $.ajax({
    66                 url:"dingdan.php",
    67                 dataType:"TEXT",
    68                 success: function(data){
    69                         if(data.trim()=="OK")
    70                         {
    71                             alert("购买成功");
    72                         }
    73                         else if(data.trim()=="YEBUZU")
    74                         {
    75                             $("#ts").html("余额不足");
    76                             $("#ts").css("color","red");
    77                         }
    78                         else
    79                         {
    80                             $("#ts").html(data);
    81                             $("#ts").css("color","red");
    82                         }
    83                     }
    84             });
    85     })
    86 </script>
    87 </body>
    88 </html>

    7.购物车页面删除处理页面shanchu.php

     1 <?php
     2 session_start();
     3 
     4 $sy = $_GET["sy"];
     5 
     6 $attr = $_SESSION["gwc"];
     7 
     8 if($attr[$sy][1]>1)
     9 {
    10     $attr[$sy][1] = $attr[$sy][1]-1;
    11 }
    12 else
    13 {
    14     unset($attr[$sy]);
    15     $attr = array_values($attr);
    16 }
    17 $_SESSION["gwc"]=$attr;
    18 
    19 header("location:gouwuche.php");
    20 
    21 8.账户余额页面zhanghu.php
    22 
    23 
    24 <?php
    25 session_start();
    26 $uid = $_SESSION['uid'];
    27 ?>
    28 
    29 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    30 <html xmlns="http://www.w3.org/1999/xhtml">
    31 <head>
    32 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    33 <title>无标题文档</title><br />
    34 <style type="text/css">
    35 .list{ 100%; height:30px; margin-top:10px; text-align:center; line-height:30px; vertical-align:middle}
    36 </style>
    37 </head>
    38 <body>
    39 <div style="100%; height:100px; background-color:#6CC">
    40     <h1 style="float:left">大苹果商城</h1>
    41     <a style="float:right; margin-top:40px" href="zhuxiao.php">注销</a>
    42 </div>
    43 <br />
    44 <div style="100%; height:600px">
    45     <div id="left" style="20%; float:left">
    46         <a href="main.php"><div class="list">浏览商品</div></a>
    47         <a href="zhanghu.php"><div class="list">查看账户</div></a>
    48         <a href="gouwuche.php"><div class="list">查看购物车</div></a>
    49     </div>    
    50     <div id="right" style="80%; height:150px; float:left">
    51 
    52 <?php
    53         include("../DBDA.class.php");
    54         $db = new DBDA();
    55         $sql = "select Account from login where UserName='{$uid}'";
    56         $result = $db->strQuery($sql);
    57         
    58         echo ("您的账户中还剩余".$result);
    59     ?>
    60 
    61     </div>
    62 </div>
    63 
    64 </body>
    65 </html>
  • 相关阅读:
    快速搭建http server
    cwmp part2 测试基础RPC
    Leetcode-5223 Queens That Can Attack the King(可以攻击国王的皇后)
    Leetcode-5222 Split a String in Balanced Strings(分割平衡字符串)
    Leetcode-5224 Dice Roll Simulation(掷骰子模拟)
    P2604-[ZJOI2010]网络扩容
    P2053-[SCOI2007]修车
    P2153-[SDOI2009]晨跑
    P2774 方格取数问题
    P2763-试题库问题
  • 原文地址:https://www.cnblogs.com/chenshanhe/p/7047327.html
Copyright © 2011-2022 走看看