Jump to content

Cart problem - adds more items on page refresh


c_shelswell

Recommended Posts

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

<?php
session_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();

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.