469,356 Members | 2,190 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

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 7355
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

Post your reply

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

Similar topics

6 posts views Thread by Fnark! | last post: by
2 posts views Thread by Don Grover | last post: by
2 posts views Thread by Paul Bruneau | last post: by
1 post views Thread by madison | last post: by
1 post views Thread by Adil Akram | last post: by
2 posts views Thread by G.E.M.P | last post: by
1 post views Thread by jecha | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.