PHP Practical Example: Shopping cart involves session with Ajax

Source: Internet
Author: User

Login

<div> user name: <input type= "text" id= "UID"/></DIV>
<div> Password: <input type= "text" id= "pwd"/ ></div>,
<input type= "button" value= "Login" 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 ("User name or password error ");
    }
    }
  });
 
 })
</script>

Loginchuli:

<?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! = "")
{
$_session["UID"]= $uid;
echo "OK";
}
Else
{
echo "NO";
}

Main

<?php
Session_Start ();
Include (".. /dbda.class.php ");
$db = new Dbda ();
?>

<style type= "Text/css" >
. list{width:100%; height:30px; margin-top:10px; text-align:center; line-height:30px; Vertical-align:middle}
</style>

<div style= "width:100%; height:100px;
&LT;H1 style= "Float:left" > Big Apple Mall <a style= "float:right; margin-top:40px "href=" zhuxiao.php "> Logout </a>
</div>
<br/>
<div style= "width:100%; height:600px ">
<div id= "left" style= "width:20%; Float:left ">
<a href= "main.php" ><div class= "List" > Browse products </div></a>
<a href= "zhanghu.php" ><div class= "List" > View account </div></a>
<a href= "gouwuche.php" ><div class= "list" > View shopping Cart </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> cart: {$zhonglei} items, Total Price: {$sum} Yuan .</div>";
?>

<table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" >
<tr>
<td> Code </td>
<td> Fruit Name </td>
<td> Fruit Price </td>
<td> Origin </td>
<td> Inventory </td>
<td> Operations </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]} ' > Purchase </a></td></tr> ';
}
?>

</table>
</div>
</div>

Goumai:

<?php
Session_Start ();
$code = $_get["code"];

if (Empty ($_session["GWC"]))
{
First click to buy
$attr = Array (
Array ($code, 1)
);
$_session["GWC"] = $attr;
}
Else
{
Not the first time you click Buy
$attr = $_session["GWC"];
$bs = 0;
foreach ($attr as $k = $v)
{
if ($v [0]== $code)
{
$bs = 1;
$attr [$k][1] = $attr [$k][1]+1;
}
}
If it does not appear inside the array
if ($bs ==0)
{
$shuzu = Array ($code, 1);
$attr [] = $shuzu;
}

$_session["GWC"]= $attr;

}
Header ("location:main.php");

Gouwuche:

<div style= "width:100%; height:100px;
&LT;H1 style= "Float:left" > Big Apple Mall <a style= "float:right; margin-top:40px "href=" zhuxiao.php "> Logout </a>
</div>
<br/>
<div style= "width:100%; height:600px ">
<div id= "left" style= "width:20%; Float:left ">
<a href= "main.php" ><div class= "List" > Browse products </div></a>
<a href= "zhanghu.php" ><div class= "List" > View account </div></a>
<a href= "gouwuche.php" ><div class= "list" > View shopping Cart </div></a>
</div>

<div id= "Right" style= "width:80%; Float:left ">
<table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" >
<tr>
<td> Product Name </td>
<td> Commodity price </td>
<td> Purchase Quantity </td>
<td> Operations </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} ' > Delete </a></td></tr> ';

}

?>

</table>
<div id= "TJ" > Submit order </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 ("Purchase Success");
}
else if (data.trim () = = "Yebuzu")
{
$ ("#ts"). HTML ("insufficient balance");
$ ("#ts"). CSS ("Color", "red");
}
Else
{
$ ("#ts"). HTML (data);
$ ("#ts"). CSS ("Color", "red");
}
}
});
})
</script>

Dingdan:

<?php
Session_Start ();
Include (".. /dbda.class.php ");
$db = new Dbda ();
$uid = $_session["UID"];
$attr = Array ();
if (!empty ($_session["GWC"))
{
$attr = $_session["GWC"];
}
See if the next two conditions are met.
$bs = true;

Determine if the balance is satisfied
Find balance based on user name
$syue = "Select account from login where Username= ' {$uid} '";
$yue = $db->strquery ($syue);

Collect the total amount according to the shopping Cart array
$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
}

Determine if the inventory is satisfied

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]} not enough stock! ";
Exit

}
}


Add orders, reduce inventory, reduce balances

if ($BS)
{
Reduced inventory
foreach ($attr as $v)
{
$sql = "Update fruit set numbers = numbers-{$v [1]} where ids= ' {$v [0]} '";
$db->query ($sql, 0);
}

Minus balance
$jianyue = "Update login set account=account-{$sum} where Username= ' {$uid} '";
$db->query ($jianyue, 0);

Add Order
$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";

Shanchu:

<?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");

PHP Practical Example: Shopping cart involves session with Ajax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.