Page 1 of 1

GET method issue

Posted: Tue Dec 01, 2009 5:06 am
by dlna
Hi,
I'm new to PHP and in order to learn the language and the concepts I'm working on a e-commerce website with a shopping cart, etc.
In this site I have items, when an item is clicked, the id of the item is sent via the GET method to the shopping cart page. Using this id i add the item to the shopping cart(table) and it works fine.

The issue is; if the user clicks the refresh button, the item is added again to the shopping cart. Do you think that disabling the refresh button or F5 button is a good option? what must i do to prevent the user from adding the item to the shopping cart when the page is refreshed?
In forms I've noticed that "(isset($_POST['Submit'])){}" is helpful but for the GET method this doesn't work accordingly.

Your help is appreciated.

Re: GET method issue

Posted: Tue Dec 01, 2009 5:16 am
by mrvijayakumar
Hi,

While u submit the form? Form values will submit again & again. So, better to pass the value to page like below,

Try this,
Ex - http://ursite.com/cart.php?total=1&rs=25$.

Re: GET method issue

Posted: Tue Dec 01, 2009 5:19 am
by dlna
I'm sorry I didn't get you. There's no form involved in the shoppingcart page.

this is the link, given in the items page.
<a href="do_shoppingcart.php?id=<?php echo "$itm_id"; ?>">

thanks,

Re: GET method issue

Posted: Tue Dec 01, 2009 10:08 am
by AbraCadaver
I would probably set a session var when entering the cart, clear the var on other pages, and then check for that var when in the cart:

Code: Select all

if(!isset($_SESSION['in_cart'])) {
    $_SESSION['in_cart'] = 1;
    // update cart
}
-Shawn

Re: GET method issue

Posted: Tue Dec 01, 2009 10:28 am
by dlna
Thank so much, it worked! :D

Re: GET method issue

Posted: Tue Dec 01, 2009 10:41 am
by AbraCadaver
dlna wrote:Thank so much, it worked! :D
Don't forget to unset($_SESSION['in_cart']) in other pages.

You might want to put the whole thing into a header or other include that is included in every page, with logic to work out if they're in the cart or not and set/unset accordingly.

-Shawn