473,408 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 3773
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('location: 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("Location: 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


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?


This is not acutally a problem. Moreover it's a usability feature.
If it's get added, the enduser may delete it.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

Jul 21 '05 #11
R. Rajesh Jeba Anbiah wrote:
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?


This is not acutally a problem. Moreover it's a usability feature.
If it's get added, the enduser may delete it.


Actually, it's easily solvable. You have two scripts - let's call them
additem.php and showcart.php

when you add an item to the cart, it calls additem.php, which does the
actual work of putting the item in the cart. But it doesn't output anything.
When it's done processing the added item, additem.php uses header() to call
showcart.php, which displays the cart. If you hit refresh at this point,
you're only refreshing showcart.php, and not processing the added item
again.

--
Tony Garcia
Web Right! Development
Riverside, CA
www.WebRightDevelopment.com
Jul 21 '05 #12
On 2005-07-19, Josef Blösl <jo**********@gmx.at> 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...


This is a common problem. The usual thing to do is send a timestamp with
the data. When you recieve a form post or vars in the get string and the
timestamp is present store all the data in the session and if you
receive the exact same data with the exact same information, then you
have a duplicate and ignore, if you have the same timestamp and
different form values you unde the first post and do the second post
instad. Or question the user and ask them to fill in the form again so
you can be sure.

Don't forget to expire info from the session if it starts getting too
large
Jul 30 '05 #13

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

Similar topics

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 I can move into another catalog and total up as...
1
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
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....
7
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
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...
0
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...
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 shopping cart from scratch, using a library of dynamic...
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 I will be implementing all kinds of AJAX...
4
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.