Home  >  Article  >  Web Front-end  >  Detailed explanation of shopping cart creation examples using Ajax and PHP session

Detailed explanation of shopping cart creation examples using Ajax and PHP session

小云云
小云云Original
2017-12-19 14:14:121618browse

This article mainly introduces Ajax combined with PHP session to create a shopping cart in detail. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.

Shopping cart web page code, the specific content is as follows:

1. Login interface login.php







无标题文档

用户名:

密码:

2. Login processing page loginchuli .php


StrQuery($sql);
if($mm==$pwd && $pwd!="")
{
  $UserName = $_POST["uid"];
  $_SESSION["uid"]=$uid;
  echo "OK";
}
else
{
  echo "NO";
}

3. Main page main.php


##







无标题文档

大苹果商城

注销


浏览商品

查看账户

查看购物车

购物车中有:{$zhonglei}种商品,总价格为:{$sum}元.

"; ?> Query($sql); foreach($attr as $v) { echo ""; } ?>
代号 水果名称 水果价格 源产地 库存量 操作
{$v[0]} {$v[1]} {$v[2]} {$v[3]} {$v[4]} 购买

4. Purchase processing page goumai.php


$v)
  {
    if($v[0]==$code)
    {
      $bs=1;
      $attr[$k][1] = $attr[$k][1]+1;
    }
  }
  //如果没有在数组里面出现
  if($bs==0)
  {
    $shuzu = array($code,1);
    $attr[] = $shuzu;
  }
  
  $_SESSION["gwc"]=$attr;
  
}
header("location:main.php");

5. On the order processing page, calculate the total price of the selected fruits and the remaining amount of fruits. dingdan.php


StrQuery($syue);
  
  //根据购物车数组取总金额
  $sum = 0;
  foreach($attr as $v)
  {
    $sql = "select price from fruit where ids='{$v[0]}'";
    $danjia = $db->StrQuery($sql);
    $sum = $sum +$danjia*$v[1];
  }
  if($yue<$sum)
  {
    $bs = false;
    echo "YEBUZU";
    exit;
  }
  
//判断库存是否满足

foreach($attr as $v)
{
  $skucun = "select name,numbers from fruit where ids='{$v[0]}'";
  $akucun = $db->Query($skucun);
  if($akucun[0][1]<$v[1])
  {
    $bs = false;
    echo "{$akucun[0][0]}库存不足!";
    exit;
    
  }
}

//添加订单,减库存,减余额

if($bs)
{
  //减库存
  foreach($attr as $v)
  {
    $sql = "update fruit set numbers = numbers-{$v[1]} where ids='{$v[0]}'";
    $db->Query($sql,0);
  }
  
  //减余额
  $jianyue="update login set account=account-{$sum} where username='{$uid}'";
  $db->Query($jianyue,0);
  
  //添加订单
  $dingdanhao = $uid+date("YmdHis");
  $t = time();
  
  $sorder = "insert into orders values('{$dingdanhao}','{$uid}','{$t}')";
  $db->Query($sorder,0);
  
  foreach($attr as $v)
  {
    $sxq = "insert into orderdetails values('','{$dingdanhao}','{$v[0]}','{$v[1]}')";
    $db->Query($sxq,0);
  }
}

echo "OK";

6. Shopping cart page








无标题文档

大苹果商城

注销


浏览商品

查看账户

查看购物车

$v) { $sql = "select name,price from fruit where ids='{$v[0]}'"; $ashuiguo = $db->Query($sql); echo ""; } ?>
商品名称 商品单价 购买数量 操作
{$ashuiguo[0][0]}{$ashuiguo[0][1]}{$v[1]}删除

提交订单

7. Shopping cart page deletion processing page shanchu. php

##

1)
{
  $attr[$sy][1] = $attr[$sy][1]-1;
}
else
{
  unset($attr[$sy]);
  $attr = array_values($attr);
}
$_SESSION["gwc"]=$attr;

header("location:gouwuche.php");

8.账户余额页面zhanghu.php








无标题文档

大苹果商城

注销


浏览商品

查看账户

查看购物车


Related recommendations:


PHP uses cookies to implement shopping cart tutorial case

JavaScript Implementation of Simple Shopping Cart Code Example

php Example Analysis of Preliminary Implementation of Shopping Cart Function

The above is the detailed content of Detailed explanation of shopping cart creation examples using Ajax and PHP session. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn