473,699 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add to cart and shopping cart not working

8 New Member
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");
  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.     }
  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.     }
  51. </script>
  52. </head>
  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");
  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>
  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>


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;
  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.     }
  62. ?>
Jan 19 '10 #1
1 7667
1,584 Recognized Expert Top Contributor
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.

Jan 19 '10 #2

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

Similar topics

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 collection of $orderline associative arrays which, in turn, hold the global POST values key 'order_code' and value 'qty' as passed in from another page. My problem is (shown by using print_r to print out the contents of the arrays) each time I...
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 im going. Just a couple of hints will do, Im familiar with vb script but not java based code , and im wondering how to store what they select so I can move around different product ranges. Don
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 storing the necessary product information. There could be several fields involved... Then there ought to be another javascript method that can create a new document based on the list a user has built selecting product from several
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 be able to come along and add it to their shopping cart. thanks joy
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 in table. All products and cart status pages are on non SSL connection. On checkout to get secure user information I shifted connection to SSL but when shifting to SSL, the SessionID changed (As is this is default behavior of IIS to prevent...
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 screen generation routines (already written) that take an XML stream as input from various "search for products" forms. That way I can run queries in one window and display the dynamic results in another. The searching functions will probably
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 like it to work properly so that a record count counts through all the books and genertates these numbers. watch out for line breaks <%@ Language=VBScript %> <% Option Explicit %> <!--#include file="DatabaseConnect.asp"-->
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 <? require_once('functions.inc.php'); session_start(); do_html_header("Checkout"); $cart = $_SESSION; if($cart&&array_count_values($cart)) { display_cart($cart,false,0); display_checkout_form($HTTP_POST_VARS);
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 functionality on. However, first I want to make sure it works if javascript is not available. So when your on the shopping cart page, you can update the quantities then click Update Cart, which would update the totals, and refresh the shopping cart page,...
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.