deucalion0
deucalion0

Reputation: 2440

Creating a simple shopping basket in PHP using session variables

I am getting very confused with ISSET, GET and SESSIONS, trying to create a simple shopping basket. I have a link "Add to basket", under each of my items, when clicking this I want to start a session for the cart, and add that item to the basket, so my item amount variable will increment to one and the price will start with the value of the selected item. I have a mini shopping cart at the top of the page where I want these values to update as items are selected.

I made a mess of my code and deleted it all to start again, before I do I was hoping for some advice, I have read up on carts and sessions for a week now. I have a database that contains products and information such as range, category and price etc. I also have links in the mini basket which will clear the basket and also open the basket in a web page to provide more detail.

Upvotes: 1

Views: 2086

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157828

you should use POST, not GET
assuming you have an item id in the $_POST['item'] variable:

session_start();
if (isset($_POST['item'])) {
    $_SESSION['cart'][] = $_POST['item'];
    header("Location: ".$_SERVER['REQUEST_URI']);
    exit;
}

Upvotes: 1

Related Questions