adi123 Posted August 17, 2010 Share Posted August 17, 2010 I have a shopping cart on my website with some minor errors. When you refresh the page it adds an extra quantity to the item when it shouldn't. Any help the code is below. $cart = $_SESSION['cart']; $action = $_GET['action']; switch ($action) { case 'add': if ($cart) { $cart .= ','.$_GET['id']; } else { $cart = $_GET['id']; } break; case 'delete': if ($cart) { $items = explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($_GET['id'] != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } $cart = $newcart; } break; case 'update': if ($cart) { $newcart = ''; foreach ($_POST as $key=>$value) { if (stristr($key,'qty')) { $id = str_replace('qty','',$key); $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($id != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } } } $cart = $newcart; break; } $_SESSION['cart'] = $cart; Quote Link to comment https://forums.phpfreaks.com/topic/210947-help-needed-with-cart-urgent/ Share on other sites More sharing options...
Adam Posted August 17, 2010 Share Posted August 17, 2010 That's happening because the parameters tell the code to do that. To stop that behaviour you need to redirect back to the cart page without the parameters. Quote Link to comment https://forums.phpfreaks.com/topic/210947-help-needed-with-cart-urgent/#findComment-1100277 Share on other sites More sharing options...
adi123 Posted August 17, 2010 Author Share Posted August 17, 2010 when i take out the parameters i get an error. is there any other way i can add the item without it increasing on refresh. Quote Link to comment https://forums.phpfreaks.com/topic/210947-help-needed-with-cart-urgent/#findComment-1100291 Share on other sites More sharing options...
JasonLewis Posted August 17, 2010 Share Posted August 17, 2010 No what MrAdam was saying was when you have added the item to the cart, redirect your user back to the cart page. $_SESSION['cart'] = $cart; // Now redirect back to cart page, so they can't refresh it. header("Location: cart.php"); I'd recommend using a form to update your cart though. Quote Link to comment https://forums.phpfreaks.com/topic/210947-help-needed-with-cart-urgent/#findComment-1100294 Share on other sites More sharing options...
adi123 Posted August 17, 2010 Author Share Posted August 17, 2010 that code is written in the cart page. I took this cart from a tutorial from this page. http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart can you download the files and check the error as i need to get my site working as quick as possible. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/210947-help-needed-with-cart-urgent/#findComment-1100298 Share on other sites More sharing options...
MadTechie Posted August 17, 2010 Share Posted August 17, 2010 Forum Guidelines 2. Users will not mark their post as being "URGENT" by either making the post ALL CAPS or adding any hint that they need it done Quick or ASAP. All posts are treated equal, your post is no more important than any other user's post on this forum. If it is "URGENT" pay someone to do it. Forum DO NOT's 5. Do not mark posts as being urgent. Quote Link to comment https://forums.phpfreaks.com/topic/210947-help-needed-with-cart-urgent/#findComment-1100300 Share on other sites More sharing options...
JasonLewis Posted August 17, 2010 Share Posted August 17, 2010 can you download the files and check the error as i need to get my site working as quick as possible. No, I could but I won't. The best we can do here is help you with your coding problems, not code for you. Quote Link to comment https://forums.phpfreaks.com/topic/210947-help-needed-with-cart-urgent/#findComment-1100302 Share on other sites More sharing options...
MadTechie Posted August 17, 2010 Share Posted August 17, 2010 ProjectFear's previous post is correct, basically you are refeshing the page with the same URL this requesting an extra item to be added! if you just redirect to the game page without the parameters all should be fine! PS read the rules it also stays we won't write the code for you! Quote Link to comment https://forums.phpfreaks.com/topic/210947-help-needed-with-cart-urgent/#findComment-1100308 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.