c_shelswell Posted November 10, 2006 Share Posted November 10, 2006 Hi I'm new here and new to PHP i'm getting stuck on what is probably a very easy solution. I've got a shopping cart i'm making that sends info via $_GET to the show_cart.php page it's all working fine only problem is that everytime you hit refresh it adds the last item to the cart again. I presume because the $_GET info is still there. I tried changing the header location so that after a user had clicked on one item the page would set itself to simply show_cart.php rather than show_cart.php?media_id=1 etc. but because i'm using sessions PHP complains that the headers are already set. I've pasted the code below if anyone has any ideas that would be great.Many thanks<?phpsession_start();require ('./login/include/session.php');include('./require/required_funcs.php');$imgUrl = "./";echo '<div id="header"><h1>Your Shopping Cart<h1></div>';page_header('Your Shopping Cart', $imgurl);@$new=$_GET['media_id'];$pic = $_GET['pic'];if ($new){ if(!isset($_SESSION['cart'])){ $_SESSION['cart'] = array(); $_SESSION['items'] = 0; $_SESSION['total_price']='0.00'; } if(isset($_SESSION['cart'][$new])) { $_SESSION['cart'][$new]++; } else $_SESSION['cart'][$new] = 1; $_SESSION['total_price']= calculate_price($_SESSION['cart']); $_SESSION['items'] = calculate_items($_SESSION['cart']); }if(isset($_POST['save'])){ foreach ($_SESSION['cart'] as $media_id => $qty){ if ($_POST[$media_id]=='0') unset($_SESSION['cart'][$media_id]); else $_SESSION['cart'][$media_id] = $_POST[$media_id]; } $_SESSION['total_price']= calculate_price($_SESSION['cart']); $_SESSION['items'] = calculate_items($_SESSION['cart']);}page_header('Your Shopping Cart '.$_SESSION['items'].' items', $imgurl);if($_SESSION['cart'] && array_count_values($_SESSION['cart'])){ display_cart($_SESSION['cart']);} else { echo '<p>There are no items in your cart</p>'; echo '<hr />';}display_button('index.php', 'continue-shopping', 'Continue Shopping');if (isset($session->username)){ $url = "./purchase.php";}else{ $url = "./login/login.php";} display_button($url, 'go-to-checkout', 'Go to Checkout');html_footer();?> Link to comment https://forums.phpfreaks.com/topic/26828-cart-problem-adds-more-items-on-page-refresh/ Share on other sites More sharing options...
blear Posted November 10, 2006 Share Posted November 10, 2006 Have an intermediate page that does the actual adding of items onto the cart. Like:itemListForm.php goes to addToCart.php which automatically goes to showShoppingCart.php with no form info submitted. Link to comment https://forums.phpfreaks.com/topic/26828-cart-problem-adds-more-items-on-page-refresh/#findComment-122698 Share on other sites More sharing options...
c_shelswell Posted November 10, 2006 Author Share Posted November 10, 2006 Cool thanks very much i'll give that a go.cheers Link to comment https://forums.phpfreaks.com/topic/26828-cart-problem-adds-more-items-on-page-refresh/#findComment-122757 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.