Create a shopping cart Using Ajax + PHP sessions and ajaxsession
The shopping cart webpage code. The specific content is as follows:
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> untitled document </title>
2. log on to the loginchuli. php page.
<?phpsession_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 on the home page
<? Phpsession_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> untitled document </title> <br/> <style type =" text/css ">. list {width: 100%; height: 30px; margin-top: 10px; text-align: center; line-height: 30px; vertical-align: middle} </style>
4. goumai. php
<? Phpsession_start (); $ code = $ _ GET ["code"]; if (empty ($ _ SESSION ["gwc"]) {// click $ attr = array ($ code, 1) for the first time; $ _ SESSION ["gwc"] = $ attr ;} else {// not the first time you click to 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 no if ($ bs = 0) appears in the array) {$ 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 fruit and the remaining fruit. Dingdan. php
<? Phpsession_start (); include (".. /DBDA. class. php "); $ db = new DBDA (); $ uid = $ _ SESSION [" uid "]; $ attr = array (); if (! Empty ($ _ SESSION ["gwc"]) {$ attr = $ _ SESSION ["gwc"] ;}// check whether both conditions meet $ bs = true; // determine whether the balance is met // find the balance based on the user name $ syue = "select account from login where username = '{$ uid }'"; $ yue = $ db-> StrQuery ($ syue); // obtain the total amount based on 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 whether the inventory meets 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]} inventory shortage! "; Exit ;}// add order, reduce inventory, and deduct balance if ($ bs) {// subtract inventory foreach ($ attr as $ v) {$ SQL = "update fruit set numbers = numbers-{$ v [1]} where ids = '{$ v [0]}'"; $ db-> Query ($ SQL, 0 );} // deduct balance $ jianyue = "update login set account = account-{$ sum} where username = '{$ uid}'"; $ db-> Query ($ jianyue, 0); // Add an 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 ";
6. Shopping Cart page
<? Phpsession_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> untitled document </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>
7. Delete shanchu. php from the shopping cart page
<? Phpsession_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. account balance page zhanghu. php <? Phpsession_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> untitled document </title> <br/> <style type =" text/css ">. list {width: 100%; height: 30px; margin-top: 10px; text-align: center; line-height: 30px; vertical-align: middle} </style>
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.