472,353 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 7556
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...
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...
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,...
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...
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...
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...
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...
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...
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.