pesky session add id in url, adds on page refresh
I have a basic link that adds a product id to the url, php then gets that id to add to the session
echo '<a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a>';
'
when the cart.php page is accessed, the php script in there adds the product to the users session, the ID is the id in the mysql database, so it knows what package to put in their session (and therefore cart)
but when it opens cart.php, which is also the page that displays the cart, the url still has the id in it
cart.php?action=add&id=1
'
So a page refresh, or if a user goes to another page then uses the back button, it adds another product to the session (and the cart)
Any way around this?
The only thought I had is have a seperate page for the actual working out of the script/session and id, then after it calculates it all then sends the user to the real cart page to display their cart
which would then remove the problem from the url
Is this the best way?
Cheers
pr0gr4mm3r posted this at 17:54 — 6th July 2007.
He has: 1,502 posts
Joined: Sep 2006
In your case, add this after your code that processes the addition of the item:
<?php
header('Location: cart.php');
?>
That will clear the $_GET data and solve your problem.
greg posted this at 17:51 — 9th July 2007.
He has: 1,581 posts
Joined: Nov 2005
hmm
The problem i am having is the cart.php has ALL the code on it, all the variables everything
So when the user clicks the link for the product they want, the whole workings from the $_GET to displaying products uses the same variables
So they all have to be on the same page
And of course using your redirect does solve the problem as the id in the url was retrieved from a page, then that redirect sends them to display their cart
but then it doesnt recognise the variables that is assigned to the $_GET when it goes to the page to display their cart contents
So it doesnt know what that id was from the $_GET to display the required products
The code i have doesnt simply $_GET then add to the session
otherwise i could simply get from the session on the next page
It uses a variable to perform a selection of possible actions
$product_id = $_GET['id']; //the product id from the URL
$action = $_GET[action]; //the action from the URL
//if there is an product_id and that product_id doesn't exist display an error message
if($product_id && !productExists($product_id)) {
die("Error. Product Doesn't Exist");
}
switch($action) {
case "add":
$_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id
break;
case "remove":
$_SESSION['cart'][$product_id]--; //remove one item
if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely to avoid quantity being -1, -2 etc upon further pressing of the link
break;
case "empty":
unset($_SESSION['cart']); //empty the cart.
break;
}
So the code adds to the session but also gives the user the chance to remove items or empty all the cart
After that code it displays the cart, obviously using the session but also the variable $product_id and the options from the switch
I dont know much about registering global variables? would this help, as I would only need it to remember $product_id
And will it remember the switch actions on another page?
The other thing.
I am still learning, but isnt this a hellish way of doing things? :..
The user clicks a link for a product. That link sends an ID to the URL, another page GETS the ID from the URL, then the script assigns the data from the GET to a variable, then it adds it to the session
There surely must be a better way of doing all this
Cant i just assign the ID value to the required variable WHEN the user clicks the link?
Cheers
pr0gr4mm3r posted this at 17:56 — 9th July 2007.
He has: 1,502 posts
Joined: Sep 2006
If you need to save one of the variables, then something like this would work.
<?php
header('Location: cart.php?id=' . $_GET['id']);
?>
greg posted this at 18:09 — 9th July 2007.
He has: 1,581 posts
Joined: Nov 2005
erm
but that sends the id to the url
So I am back where I started, but now with another page inbetween
The point is to get to the cart to display the users cart contents without the ID=1 in the URL
pr0gr4mm3r posted this at 18:18 — 9th July 2007.
He has: 1,502 posts
Joined: Sep 2006
I thought that meant you wanted to get rid of the $action variable and keep the $id variable...so I'm confused.
Maybe it would be best to have the processing page separate from the cart page. In other words, the link to add the product will go to like maybe add_item.php. Then, redirect them to cart.php.
greg posted this at 20:03 — 9th July 2007.
He has: 1,581 posts
Joined: Nov 2005
I was wroking on seperate pages as you mentioned, but as I explained above, the $_GET['id'] from the url is assigned to a variable, that variable is used within the cart contents and session calculations
so the alternative I thought about is still have all the variables and cart session caclulations on the one page, as they were originaly, and change the link that users use to select the product they are choosing:
'Add to cart';
and maybe use a form?
now forms are fairly straight forward, and i presume i would use hidden data to submit the variable value to the next page
where the next page would still have the get, but would now be $_GET['form_input_name']
The products are listed into a table from the database in a standard loop
So the ID of the product is determined via the $row['id']
Each row has one product on it, the product is of course the ['id']
The bit i'm stuck with, how do i put into a form something like:
echo "<input type="hidden" name="var1" value="<?php $_POST['.$row['id']']; ?>"
'cheers
greg posted this at 21:33 — 9th July 2007.
He has: 1,581 posts
Joined: Nov 2005
Ok thanks, I have it sorted.
I broke out of PHP to make the form, that way I could understand the sytnax better
And also will keep server usage down!
For those interested, and others it may help, I'll explain what I did:
<form action="session.php" method="post">
<input type="hidden" name="check" value="addpack">
<input type="hidden" name="packadd" value="<?php echo $row['id']; ?>">
<input type="submit" name="submit" value="Add to Cart"/>
</form>
Because I have two other options for the users cart, remove one item and empty whole cart, I needed someway of tackling this altogether.
So I used one page, sessions.php
And to incoporate them all in one file I included in the form for the package add, delete and cart empty:
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.