這篇文章主要介紹“如何用Ajax + PHP session制作購物車”,在日常操作中,相信很多人在如何用Ajax + PHP session制作購物車問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何用Ajax + PHP session制作購物車”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)成立于2013年,我們提供高端重慶網(wǎng)站建設(shè)、成都網(wǎng)站制作、網(wǎng)站設(shè)計、網(wǎng)站定制、成都全網(wǎng)營銷推廣、小程序開發(fā)、微信公眾號開發(fā)、seo優(yōu)化服務(wù),提供專業(yè)營銷思路、內(nèi)容策劃、視覺設(shè)計、程序開發(fā)來完成項(xiàng)目落地,為成都電動窗簾企業(yè)提供源源不斷的流量和訂單咨詢。
購物車網(wǎng)頁代碼,具體內(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>無標(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("用戶名或密碼錯誤"); } } }) }) </script> </html>
2.登錄處理頁面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.主頁面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>無標(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 > <h2 >大蘋果商城</h2> <a href="zhuxiao.php">注銷</a> </div> <br /> <div > <div id="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">查看購物車</div></a> </div> <div id="right" > <?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>購物車中有:{$zhonglei}種商品,總價格為:{$sum}元.</div>"; ?> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代號</td> <td>水果名稱</td> <td>水果價格</td> <td>源產(chǎn)地</td> <td>庫存量</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]}'>購買</a></td></tr>"; } ?> </table> </div> </div> </body> </html>
4.購買處理頁面goumai.php
<?php session_start(); $code = $_GET["code"]; if(empty($_SESSION["gwc"])) { //第一次點(diǎn)擊購買 $attr = array( array($code,1) ); $_SESSION["gwc"] = $attr; } else { //不是第一次點(diǎn)擊購買 $attr = $_SESSION["gwc"]; $bs=0; foreach($attr as $k=>$v) { if($v[0]==$code) { $bs=1; $attr[$k][1] = $attr[$k][1]+1; } } //如果沒有在數(shù)組里面出現(xiàn) if($bs==0) { $shuzu = array($code,1); $attr[] = $shuzu; } $_SESSION["gwc"]=$attr; } header("location:main.php");
5.訂單處理頁面,計算選取水果的總價,和水果剩余量。dingdan.php
<?php session_start(); include("../DBDA.class.php"); $db = new DBDA(); $uid = $_SESSION["uid"]; $attr = array(); if(!empty($_SESSION["gwc"])) { $attr = $_SESSION["gwc"]; } //看下兩個條件是否都滿足 $bs = true; //判斷余額是否滿足 //根據(jù)用戶名找余額 $syue = "select account from login where username='{$uid}'"; $yue = $db->StrQuery($syue); //根據(jù)購物車數(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; } //判斷庫存是否滿足 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.購物車頁面
<?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>無標(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 > <h2 >大蘋果商城</h2> <a href="zhuxiao.php">注銷</a> </div> <br /> <div > <div id="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">查看購物車</div></a> </div> <div id="right" > <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>商品名稱</td> <td>商品單價</td> <td>購買數(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("購買成功"); } else if(data.trim()=="YEBUZU") { $("#ts").html("余額不足"); $("#ts").css("color","red"); } else { $("#ts").html(data); $("#ts").css("color","red"); } } }); }) </script> </body> </html>
7.購物車頁面刪除處理頁面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.賬戶余額頁面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>無標(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 > <h2 >大蘋果商城</h2> <a href="zhuxiao.php">注銷</a> </div> <br /> <div > <div id="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">查看購物車</div></a> </div> <div id="right" > <?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>
到此,關(guān)于“如何用Ajax + PHP session制作購物車”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)頁標(biāo)題:如何用Ajax+PHPsession制作購物車
鏈接URL:http://jinyejixie.com/article16/posjdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、網(wǎng)站策劃、關(guān)鍵詞優(yōu)化、網(wǎng)站制作、建站公司、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)