473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Add to cart and shopping cart not working

8
Hi all pls I'm having great difficulty in making my shopping cart work. I am a newbie in php with little understanding of it. Add to cart button doesn't work and shopping cart in general doesn't. Pls help. Below is the code for both shopping cart, products and function.php

shopping cart code:

Expand|Select|Wrap|Line Numbers
  1. <?
  2.     include("db.php");
  3.     include("functions.php");
  4.  
  5.     if($_REQUEST['command']=='delete' && $_REQUEST['id']>0){
  6.         remove_product($_REQUEST['id']);
  7.     }
  8.     else if($_REQUEST['command']=='clear'){
  9.         unset($_SESSION['cart']);
  10.     }
  11.     else if($_REQUEST['command']=='update'){
  12.         $max=count($_SESSION['cart']);
  13.         for($i=0;$i<$max;$i++){
  14.             $id=$_SESSION['cart'][$i]['id'];
  15.             $q=intval($_REQUEST['Bags'.$id]);
  16.             if($q>0 && $q<=999){
  17.                 $_SESSION['cart'][$i]['qty']=$q;
  18.             }
  19.             else{
  20.                 $msg='Some products not updated!, quantity must be a number between 1 and 999';
  21.             }
  22.         }
  23.     }
  24.  
  25. ?>
  26. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  27. <html xmlns="http://www.w3.org/1999/xhtml">
  28. <head>
  29. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  30. <title>Shopping Cart</title>
  31. <script language="javascript">
  32.     function del(pid){
  33.         if(confirm('Do you really mean to delete this item')){
  34.             document.form1.id.value=id;
  35.             document.form1.command.value='delete';
  36.             document.form1.submit();
  37.         }
  38.     }
  39.     function clear_cart(){
  40.         if(confirm('This will empty your shopping cart, continue?')){
  41.             document.form1.command.value='clear';
  42.             document.form1.submit();
  43.         }
  44.     }
  45.     function update_cart(){
  46.         document.form1.command.value='update';
  47.         document.form1.submit();
  48.     }
  49.  
  50.  
  51. </script>
  52. </head>
  53.  
  54. <body>
  55. <form name="form1" method="post">
  56. <input type="hidden" name="id" />
  57. <input type="hidden" name="command" />
  58.     <div style="margin:0px auto; width:600px;" >
  59.     <div style="padding-bottom:10px">
  60.         <h1 align="center">Your Shopping Cart</h1>
  61.     <input type="button" value="Continue Shopping" onclick="window.location='products.php'" />
  62.     </div>
  63.         <div style="color:#F00"><?=$msg?></div>
  64.         <table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
  65.         <?
  66.             if(is_array($_SESSION['cart'])){
  67.                 echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Id</td><td>Name</td><td>Price</td><td>Qty</td><td>Amount</td><td>Options</td></tr>';
  68.                 $max=count($_SESSION['cart']);
  69.                 for($i=0;$i<$max;$i++){
  70.                     $id=$_SESSION['cart'][$i]['id'];
  71.                     $q=$_SESSION['cart'][$i]['qty'];
  72.                     $bag_name=get_bag_name($id);
  73.                     if($q==0) continue;
  74.             ?>
  75.                     <tr bgcolor="#FFFFFF"><td><?=$i+1?></td><td><?=$bag_name?></td>
  76.                     <td>$ <?=get_price($id)?></td>
  77.                     <td><input type="text" name="Bags<?=$id?>" value="<?=$q?>" maxlength="3" size="2" /></td>                    
  78.                     <td>$ <?=get_price($id)*$q?></td>
  79.                     <td><a href="javascript:del(<?=$id?>)">Remove</a></td></tr>
  80.             <?                    
  81.                 }
  82.             ?>
  83.                 <tr><td><b>Order Total: $<?=get_order_total()?></b></td><td colspan="5" align="right"><input type="button" value="Clear Cart" onclick="clear_cart()"><input type="button" value="Update Cart" onclick="update_cart()"><input type="button" value="Place Order" onclick="window.location='billing.php'"></td></tr>
  84.             <?
  85.             }
  86.             else{
  87.                 echo "<tr bgColor='#FFFFFF'><td>There are no items in your shopping cart!</td>";
  88.             }
  89.         ?>
  90.         </table>
  91.     </div>
  92. </form>
  93. </body>
  94. </html>


Products code:

Expand|Select|Wrap|Line Numbers
  1. <?
  2.     include("db.php");
  3.     include("functions.php");
  4.  
  5.     if($_REQUEST['command']=='add' && $_REQUEST['id']>0){
  6.         $id=$_REQUEST['id'];
  7.         addtocart($pid,1);
  8.         header("location:shoppingcart.php");
  9.         exit();
  10.     }
  11. ?>
  12. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  13. <html xmlns="http://www.w3.org/1999/xhtml">
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  16. <title>Products</title>
  17. <script language="javascript">
  18.     function addtocart(id){
  19.         document.form1.productid.value=id;
  20.         document.form1.command.value='add';
  21.         document.form1.submit();
  22.     }
  23. </script>
  24. </head>
  25.  
  26.  
  27. <body>
  28. <form name="form1">
  29.     <input type="hidden" name="id" />
  30.     <input type="hidden" name="command" />
  31. </form>
  32. <div align="center">
  33.     <h1 align="center">Bags</h1>
  34.     <table border="0" cellpadding="2px" width="600px">
  35.         <?
  36.             $result=mysql_query("select * from Bags");
  37.             while($row=mysql_fetch_array($result)){
  38.         ?>
  39.         <tr>
  40.             <td><img src="<?=$row['picture']?>" /></td>
  41.             <td>       <b><?=$row['bag_name']?></b><br />
  42.                     <?=$row['designer']?><br />
  43.                     Price:<big style="color:green">
  44.                         $<?=$row['price']?></big><br /><br />
  45.                    <input type="button" value="Add to Cart" onclick="addtocart(<?=$row['id']?>)" />            </td>
  46.         </tr>
  47.         <tr><td colspan="2"><hr size="1" /></td>
  48.         <? } ?>
  49.     </table>
  50. </div>
  51. </body>
  52. </html>

function.php

Expand|Select|Wrap|Line Numbers
  1. <?
  2.     function get_product_name($id){
  3.         $result=mysql_query("select bag_name from Bags where id=$id");
  4.         $row=mysql_fetch_array($result);
  5.         return $row['name'];
  6.     }
  7.     function get_price($id){
  8.         $result=mysql_query("select price from Bags where id=$id");
  9.         $row=mysql_fetch_array($result);
  10.         return $row['price'];
  11.     }
  12.     function remove_product($id){
  13.         $id=intval($id);
  14.         $max=count($_SESSION['cart']);
  15.         for($i=0;$i<$max;$i++){
  16.             if($id==$_SESSION['cart'][$i]['id']){
  17.                 unset($_SESSION['cart'][$i]);
  18.                 break;
  19.             }
  20.         }
  21.         $_SESSION['cart']=array_values($_SESSION['cart']);
  22.     }
  23.     function get_order_total(){
  24.         $max=count($_SESSION['cart']);
  25.         $sum=0;
  26.         for($i=0;$i<$max;$i++){
  27.             $id=$_SESSION['cart'][$i]['id'];
  28.             $q=$_SESSION['cart'][$i]['qty'];
  29.             $price=get_price($id);
  30.             $sum+=$price*$q;
  31.         }
  32.         return $sum;
  33.     }
  34.     function addtocart($id,$q){
  35.         if($id<1 or $q<1) return;
  36.  
  37.         if(is_array($_SESSION['cart'])){
  38.             if(product_exists($id)) return;
  39.             $max=count($_SESSION['cart']);
  40.             $_SESSION['cart'][$max]['id']=$id;
  41.             $_SESSION['cart'][$max]['qty']=$q;
  42.         }
  43.         else{
  44.             $_SESSION['cart']=array();
  45.             $_SESSION['cart'][0]['id']=$id;
  46.             $_SESSION['cart'][0]['qty']=$q;
  47.         }
  48.     }
  49.     function product_exists($id){
  50.         $id=intval($id);
  51.         $max=count($_SESSION['cart']);
  52.         $flag=0;
  53.         for($i=0;$i<$max;$i++){
  54.             if($id==$_SESSION['cart'][$i]['id']){
  55.                 $flag=1;
  56.                 break;
  57.             }
  58.         }
  59.         return $flag;
  60.     }
  61.  
  62. ?>
Jan 19 '10 #1
1 7646
dlite922
1,584 Expert 1GB
Can you point us to where the problem is in the code?

I don't have time to troubleshoot your code, but can answer specific questions.

Please use code tags in the future.




Dan
Jan 19 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Fnark! | last post by:
I am creating a shopping cart using PHP Version 4.1.2. I am creating and registering a cart object in a session. The cart object contains an array of arrays called $order whose elements are a...
2
by: Don Grover | last post by:
I am retrieving costs and product id's from a sql db. and need to build a shopping cart around it. How do I store the selected items and qty req so I can move into another catalog and total up as...
2
by: Paul Bruneau | last post by:
Hi, I hope someone can help me make a working shopping cart, as a learning tool. If I have a "Product Demo" html page with a "Buy Me" button, there must be a simple javascript method of...
1
by: madison | last post by:
Hi, I am trying to start a website using paypals shopping cart function. If i have 10 items and they sell out, how do I make it so the item is then listed as sold out. The next person would not...
1
by: Adil Akram | last post by:
I have created a site shopping cart in ASP.net. I am using ASP session object's SessionID on non SSL connection to track session. While adding products to cart DB I insert product and SessionID...
2
by: G.E.M.P | last post by:
High Level Session Handling Design for a Shopping cart 0) What am I missing? 1) How does OSCommerce do it? I'm thinking about building a shopping cart from scratch, using a library of dynamic...
7
by: isaac2004 | last post by:
hi i have a basic asp page that acts as an online bookstore. on my cart page i am having trouble generating 3 numbers; a subtotal, a shipping total, and a final price. here is my code i would...
1
by: jecha | last post by:
I'm implementing a shopping cart but am having a problem in checking out a person who has added item in his/her shopping busket.The code for the checkout.php script is given below <?...
15
gregerly
by: gregerly | last post by:
Hello, I once again turn to this community of genius' for some help with a problem. I've got a shopping cart (which is working well so far) that I will be implementing all kinds of AJAX...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.