473,699 Members | 2,664 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 7666
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: 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: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
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.