473,788 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

add item to shopping cart problem

hi folks!

i have a cart link in all of my listed products on the site. when i
press this link i will generate a url like this

http://www.mysite.com/index.htm?loca...rt&productid=3

after i press this link the site cart.php will be included and add the
wanted product to the cart, and show the cart.

my problem now is after the cart is showed with the new product in it,
when i press the F5 key in the browser to refresh the site, the same
product will added to the cart once more, because the previous generated
url is unchanged in the browser.

have anyone a idea how to fix this problem?

thanx...
Jul 21 '05 #1
12 3809
Josef Blösl wrote:
hi folks!

i have a cart link in all of my listed products on the site. when i
press this link i will generate a url like this

http://www.mysite.com/index.htm?loca...rt&productid=3
after i press this link the site cart.php will be included and add the
wanted product to the cart, and show the cart.

my problem now is after the cart is showed with the new product in it,
when i press the F5 key in the browser to refresh the site, the same
product will added to the cart once more, because the previous generated
url is unchanged in the browser.

have anyone a idea how to fix this problem?

thanx...


Yes - attach a random cookie (not a web cookie) to each request and ignore
requests where the cookie is already in the basket.

yawn.

C.
Jul 21 '05 #2
Colin McKinnon schrieb:
Josef Blösl wrote:

hi folks!

i have a cart link in all of my listed products on the site. when i
press this link i will generate a url like this


http://www.mysite.com/index.htm?loca...rt&productid=3
after i press this link the site cart.php will be included and add the
wanted product to the cart, and show the cart.

my problem now is after the cart is showed with the new product in it,
when i press the F5 key in the browser to refresh the site, the same
product will added to the cart once more, because the previous generated
url is unchanged in the browser.

have anyone a idea how to fix this problem?

thanx...

Yes - attach a random cookie (not a web cookie) to each request and ignore
requests where the cookie is already in the basket.

yawn.

C.


thanks a lot... good idea
Jul 21 '05 #3
Colin,
If you're so bored, then why not give an answer that might help him
improve his code, instead of just dodge a bug.

Josef,
Ultimately, your underlying problem is that you have an "action"
variable. You need to be separating out your action code from your
display code.

Think of each page in PHP as a method in an object. Instead of having
one page, and choosing the behavior based on a switch statement, take
the time to separate out actions to dedicated pages.

In this case, create a cart-additem.php page and a cart-display.php
page. Have cart-additem.php do variable/access validation, and then add
the item to the cart. Then, relocate to the display page.

You will find that this code will be easier to maintain, and will have
few/no state-related bugs.

~D

Jul 21 '05 #4
Colin,
If you're so bored, then why not give an answer that might help him
improve his code, instead of just dodge a bug.

Josef,
Ultimately, your underlying problem is that you have an "action"
variable. You need to be separating out your action code from your
display code.

Think of each page in PHP as a method in an object. Instead of having
one page, and choosing the behavior based on a switch statement, take
the time to separate out actions to dedicated pages.

In this case, create a cart-additem.php page and a cart-display.php
page. Have cart-additem.php do variable/access validation, and then add
the item to the cart. Then, relocate to the display page.

You will find that this code will be easier to maintain, and will have
few/no state-related bugs.

~D

Jul 21 '05 #5
You need to separate your action code from your display code. You
should never have an action variable or mode variable. Ever.

Each page in PHP is like a function or method in other languages. Each
one should have a unique, specific purpose.

Create a link that goes to the cart-additem.php page.
Have cart-additem.php validate your variables, add the item to the
cart, and then relocate to a cart-display.php page.

Writing your own shopping cart can be a real dangerous thing to do.
Crackers often see custom carts as ripe targets for attack. Harden,
harden, harden your code.

~D

Jul 21 '05 #6
dracolytch schrieb:
You need to separate your action code from your display code. You
should never have an action variable or mode variable. Ever.

Each page in PHP is like a function or method in other languages. Each
one should have a unique, specific purpose.

Create a link that goes to the cart-additem.php page.
Have cart-additem.php validate your variables, add the item to the
cart, and then relocate to a cart-display.php page.

Writing your own shopping cart can be a real dangerous thing to do.
Crackers often see custom carts as ripe targets for attack. Harden,
harden, harden your code.

~D


first thank you for your post!

i understand what you mean, but how can i say (inside my addtocart.php
script) to the browser he should automatically switch to the next page
(showcart.php)
Jul 21 '05 #7
Sorry about the multiple posts... My 'net connection at work keeps
timing out, and leads me to believe responses didn't post. Ugh.

~D

Jul 21 '05 #8
Oh, here's the redirect code you're looking for:

header('locatio n: showcart.php');
die();

The die(); at the end ensures termination of the current page. This is
especially useful if you're doing validation checking, and found a
problem. This will redirect you to a new page, and prevent anything
else in the current page from being executed.

~D

Jul 21 '05 #9
JDS
On Tue, 19 Jul 2005 14:28:35 +0200, Josef Blösl wrote:
have anyone a idea how to fix this problem?


There are several ways.

One is to include some sort of tracking variable and only do the update if
the tracking variable is flagged correctly.

Another, and the way I would do it, is to use a different PHP script
entirely to do the actual form processing, and send the browser to the
correct "results" page using header("Locatio n: blah.php");

Thus you would have an invisible processing-only PHP script that redirects
the user on completion to the proper results page.

I hope this makes sense.

--
JDS | je*****@example .invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2869
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
1
1822
by: Jia Sun | last post by:
hello , everybody , i need a similar program , just like fancyimport.com if possible, pls contact me ,thank you very much . inchina@gmail.com
9
2339
by: Penny | last post by:
Hi all, I've built an online shopping cart using ASP Classic(based on a 'WebThang' tutorial). The shop cart page (with table showing customers selected items and costs) has only 3 buttons/links. 1: Back to Shop: Uses 'javascript:history.go(-1)' to go to previous page. 2: Remove Item(one next to each item in cart): Clears the selected item from the session variable array redisplays the page.
7
1893
by: Marc Bishop | last post by:
Hi can anyone help? I'm making a shopping cart and am stuck on removing an item from my array? The array is made : cArray(ITEM_NAME,cItem) = ProductName
1
3230
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...
0
1240
by: Tulasi | last post by:
Hello, any one help me the problem due to Shopping cart. I am developping a project in that project,I want to connect shopping Carts in Vb.net.The shopping carts like(www.oscommerce.com),(www.miva.com) and Yahoo! Stores.And Retrievs the Shopping cart Coustmer information and Product information Etc. how it is connected?How the information is retrived those Shopping cart? What is the procedure to connecect those Shopping Carts?Any one to...
2
2301
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
15
4299
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 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,...
4
6268
by: chrism | last post by:
Hello, I'm hoping someone can help me out here. I'm building a shopping cart using sessions, populating with $_POST values from a form. Code abbreviated below: check if form is submitted. if so, set up variables: $name = $_POST; $ribbon = $_POST;
0
9656
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, 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...
0
10370
Oralloy
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...
1
10113
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,...
0
9969
tracyyun
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...
0
8995
agi2029
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...
1
7519
isladogs
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...
0
5402
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...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
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 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.