Ajax + PHP session制作購(gòu)物車
購(gòu)物車網(wǎng)頁(yè)代碼,具體內(nèi)容如下
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>無(wú)標(biāo)題文檔</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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;
}
else
{
alert("用戶名或密碼錯(cuò)誤");
}
}
})
})
</script>
</html>
2.登錄處理頁(yè)面loginchuli.php
<?php
session_start();
include("../DBDA.class.php");
$db = new DBDA();
$uid = $_POST["u"];
$pwd = $_POST["p"];
$sql = "select password from login where username='{$uid}'";
$mm = $db->StrQuery($sql);
if($mm==$pwd && $pwd!="")
{
$UserName = $_POST["uid"];
$_SESSION["uid"]=$uid;
echo "OK";
}
else
{
echo "NO";
}
3.主頁(yè)面main.php
<?php
session_start();
include("../DBDA.class.php");
$db = new DBDA();
?>
<!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" />
<title>無(wú)標(biāo)題文檔</title><br />
<style type="text/css">
.list{ width:100%; height:30px; margin-top:10px; text-align:center; line-height:30px; vertical-align:middle}
</style>
</head>
<body>
<div style="width:100%; height:100px; background-color:#6CC">
<h1 style="float:left">大蘋果商城</h1>
<a style="float:right; margin-top:40px" href="zhuxiao.php">注銷</a>
</div>
<br />
<div style="width:100%; height:600px">
<div id="left" style="width:20%; float:left">
<a href="main.php"><div class="list">瀏覽商品</div></a>
<a href="zhanghu.php"><div class="list">查看賬戶</div></a>
<a href="gouwuche.php"><div class="list">查看購(gòu)物車</div></a>
</div>
<div id="right" style="width:80%; float:left">
<?php
$agwc = array();
if(!empty($_SESSION["gwc"]))
{
$agwc = $_SESSION["gwc"];
}
$zhonglei = count($agwc);
$sum = 0;
foreach($agwc as $v)
{
$sql = "select price from fruit where ids='{$v[0]}'";
$danjia = $db->StrQuery($sql);
$sum = $sum +$danjia*$v[1];
}
echo "<div>購(gòu)物車中有:{$zhonglei}種商品,總價(jià)格為:{$sum}元.</div>";
?>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>代號(hào)</td>
<td>水果名稱</td>
<td>水果價(jià)格</td>
<td>源產(chǎn)地</td>
<td>庫(kù)存量</td>
<td>操作</td>
</tr>
<?php
$sql = "select * from fruit";
$attr = $db->Query($sql);
foreach($attr as $v)
{
echo "<tr><td>{$v[0]}</td>
<td>{$v[1]}</td>
<td>{$v[2]}</td>
<td>{$v[3]}</td>
<td>{$v[4]}</td>
<td><a href='goumai.php?code={$v[0]}'>購(gòu)買</a></td></tr>";
}
?>
</table>
</div>
</div>
</body>
</html>
4.購(gòu)買處理頁(yè)面goumai.php
<?php
session_start();
$code = $_GET["code"];
if(empty($_SESSION["gwc"]))
{
//第一次點(diǎn)擊購(gòu)買
$attr = array(
array($code,1)
);
$_SESSION["gwc"] = $attr;
}
else
{
//不是第一次點(diǎn)擊購(gòu)買
$attr = $_SESSION["gwc"];
$bs=0;
foreach($attr as $k=>$v)
{
if($v[0]==$code)
{
$bs=1;
$attr[$k][1] = $attr[$k][1]+1;
}
}
//如果沒(méi)有在數(shù)組里面出現(xiàn)
if($bs==0)
{
$shuzu = array($code,1);
$attr[] = $shuzu;
}
$_SESSION["gwc"]=$attr;
}
header("location:main.php");
5.訂單處理頁(yè)面,計(jì)算選取水果的總價(jià),和水果剩余量。dingdan.php
<?php
session_start();
include("../DBDA.class.php");
$db = new DBDA();
$uid = $_SESSION["uid"];
$attr = array();
if(!empty($_SESSION["gwc"]))
{
$attr = $_SESSION["gwc"];
}
//看下兩個(gè)條件是否都滿足
$bs = true;
//判斷余額是否滿足
//根據(jù)用戶名找余額
$syue = "select account from login where username='{$uid}'";
$yue = $db->StrQuery($syue);
//根據(jù)購(gòu)物車數(shù)組取總金額
$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;
}
//判斷庫(kù)存是否滿足
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]}庫(kù)存不足!";
exit;
}
}
//添加訂單,減庫(kù)存,減余額
if($bs)
{
//減庫(kù)存
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.購(gòu)物車頁(yè)面
<?php
session_start();
?>
<!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" />
<title>無(wú)標(biāo)題文檔</title><br />
<style type="text/css">
.list{ width:100%; height:30px; margin-top:10px; text-align:center; line-height:30px; vertical-align:middle}
</style>
<script src="../../jquery-1.11.2.min.js"></script>
</head>
<body>
<div style="width:100%; height:100px; background-color:#6CC">
<h1 style="float:left">大蘋果商城</h1>
<a style="float:right; margin-top:40px" href="zhuxiao.php">注銷</a>
</div>
<br />
<div style="width:100%; height:600px">
<div id="left" style="width:20%; float:left">
<a href="main.php"><div class="list">瀏覽商品</div></a>
<a href="zhanghu.php"><div class="list">查看賬戶</div></a>
<a href="gouwuche.php"><div class="list">查看購(gòu)物車</div></a>
</div>
<div id="right" style="width:80%; float:left">
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>商品名稱</td>
<td>商品單價(jià)</td>
<td>購(gòu)買數(shù)量</td>
<td>操作</td>
</tr>
<?php
include("../DBDA.class.php");
$db = new DBDA();
$attr=array();
if(!empty($_SESSION["gwc"]))
{
$attr = $_SESSION["gwc"];
}
foreach($attr as $k=>$v)
{
$sql = "select name,price from fruit where ids='{$v[0]}'";
$ashuiguo = $db->Query($sql);
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>";
}
?>
</table>
<div id="tj">提交訂單</div><div id="ts"></div>
</div>
</div>
<script type="text/javascript">
$("#tj").click(function(){
$.ajax({
url:"dingdan.php",
dataType:"TEXT",
success: function(data){
if(data.trim()=="OK")
{
alert("購(gòu)買成功");
}
else if(data.trim()=="YEBUZU")
{
$("#ts").html("余額不足");
$("#ts").css("color","red");
}
else
{
$("#ts").html(data);
$("#ts").css("color","red");
}
}
});
})
</script>
</body>
</html>
7.購(gòu)物車頁(yè)面刪除處理頁(yè)面shanchu.php
<?php
session_start();
$sy = $_GET["sy"];
$attr = $_SESSION["gwc"];
if($attr[$sy][1]>1)
{
$attr[$sy][1] = $attr[$sy][1]-1;
}
else
{
unset($attr[$sy]);
$attr = array_values($attr);
}
$_SESSION["gwc"]=$attr;
header("location:gouwuche.php");
8.賬戶余額頁(yè)面zhanghu.php
<?php
session_start();
$uid = $_SESSION['uid'];
?>
<!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" />
<title>無(wú)標(biāo)題文檔</title><br />
<style type="text/css">
.list{ width:100%; height:30px; margin-top:10px; text-align:center; line-height:30px; vertical-align:middle}
</style>
</head>
<body>
<div style="width:100%; height:100px; background-color:#6CC">
<h1 style="float:left">大蘋果商城</h1>
<a style="float:right; margin-top:40px" href="zhuxiao.php">注銷</a>
</div>
<br />
<div style="width:100%; height:600px">
<div id="left" style="width:20%; float:left">
<a href="main.php"><div class="list">瀏覽商品</div></a>
<a href="zhanghu.php"><div class="list">查看賬戶</div></a>
<a href="gouwuche.php"><div class="list">查看購(gòu)物車</div></a>
</div>
<div id="right" style="width:80%; height:150px; float:left">
<?php
include("../DBDA.class.php");
$db = new DBDA();
$sql = "select Account from login where UserName='{$uid}'";
$result = $db->strQuery($sql);
echo ("您的賬戶中還剩余".$result);
?>
</div>
</div>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- PHP實(shí)現(xiàn)負(fù)載均衡下的session共用功能
- PHP實(shí)現(xiàn)分布式memcache設(shè)置web集群session同步的方法
- PHP+Session防止表單重復(fù)提交的解決方法
- redis 替代php文件存儲(chǔ)session的實(shí)例
- php基于session鎖防止阻塞請(qǐng)求的方法分析
- PHP編程中的Session阻塞問(wèn)題與解決方法分析
- 詳解PHP使用Redis存儲(chǔ)session時(shí)的一個(gè)Warning定位
- php如何修改SESSION的生存存儲(chǔ)時(shí)間的實(shí)例代碼
- 深入淺析PHP的session反序列化漏洞問(wèn)題
- PHP session實(shí)現(xiàn)購(gòu)物車功能
- PHP學(xué)習(xí)筆記之session
相關(guān)文章
AJax 把拿到的后臺(tái)數(shù)據(jù)在頁(yè)面中渲染的實(shí)例
今天小編就為大家分享一篇AJax 把拿到的后臺(tái)數(shù)據(jù)在頁(yè)面中渲染的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
利用Ajax實(shí)現(xiàn)智能回答的機(jī)器人示例代碼
這篇文章主要介紹了利用Ajax實(shí)現(xiàn)智能回答的機(jī)器人,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
你的jquery ajax無(wú)效和你的jquery引入路徑有關(guān)
當(dāng)你發(fā)現(xiàn)你的jquery ajax無(wú)效的時(shí)候,不妨使用 fire bug調(diào)試一下,這時(shí)你會(huì)發(fā)現(xiàn),提示"$"無(wú)效,為什么會(huì)有這種提示呢?可能是你引入jquery.js的路徑有問(wèn)題2013-06-06
ajax 怎么設(shè)置超時(shí)(一個(gè)action執(zhí)行了2遍)
一個(gè)action執(zhí)行了2遍 ,這個(gè)操作大致需要5分多鐘才能完成,于是搜了幾個(gè)可以設(shè)置超時(shí)的地方,希望對(duì)大家有所幫助2014-08-08
Ajax讀取數(shù)據(jù)到表格的實(shí)現(xiàn)代碼
前兩篇我們講了"ajax開(kāi)始的準(zhǔn)備"與及如何使用ajax技術(shù)進(jìn)行簡(jiǎn)單的數(shù)據(jù)讀?。裉煳覀円v的是:使用Ajax無(wú)刷新技術(shù)讀取服務(wù)端多條數(shù)據(jù),并將返回的數(shù)據(jù)顯示到一個(gè)表格內(nèi).2010-08-08

