0
votes

I have the following code which is not working. I have a problem with my session based shopping cart. When i add stuff, it doesn't show up, and i don't know why.

main.php file, first few lines, they receive the infos to add stuff to the cart:

//Start the session
session_start();

//Create 'cart' if it doesn't already exist
if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); }


//Add an item only if we have the required pices of information: name, qty
if (isset($_GET['name']) && isset($_GET['qty']) && isset($_GET['add'])){
    //Adding an Item
    //Store it in a Array
    $ITEM = array(
        //Item name     
        'name' => $_GET['name'], 
        //Item Price
        //'price' => $_GET['price'], 
        //Qty wanted of item
        'qty' => $_GET['qty']       
        );

    //Add this item to the shopping cart
    $_SESSION['SHOPPING_CART'][] =  $ITEM;
    //Clear the URL variables
    header('Location: ' . $_SERVER['PHP_SELF']);
}
//Allowing the modification of individual items no longer keeps this a simple shopping cart.
//We only support emptying and removing
else if (isset($_GET['remove'])){
    //Remove the item from the cart
    unset($_SESSION['SHOPPING_CART'][$_GET['remove']]);
    //Re-organize the cart
    //array_unshift ($_SESSION['SHOPPING_CART'], array_shift ($_SESSION['SHOPPING_CART']));
    //Clear the URL variables
    header('Location: ' . $_SERVER['PHP_SELF'].'#cart');

}
else if (isset($_GET['empty'])){
    //Clear Cart by destroying all the data in the session
    session_destroy();
    //Clear the URL variables
    header('Location: ' . $_SERVER['PHP_SELF'].'#cart');

}
else if (isset($_POST['update'])) {
    //Updates Qty for all items
    foreach ($_POST['items_qty'] as $itemID => $qty) {
        //If the Qty is "0" remove it from the cart
        if ($qty == 0) {
            //Remove it from the cart
            unset($_SESSION['SHOPPING_CART'][$itemID]);
        }
        else if($qty >= 1) {
            //Update to the new Qty
            $_SESSION['SHOPPING_CART'][$itemID]['qty'] = $qty;
        }
    }
    //Clear the POST variables
    header('Location: ' . $_SERVER['PHP_SELF'].'#cart');
} 

?>

This is the div that SHOULD show the content of the cart, but doesn't, it's empty, not sure why. It's also inside the main.php

<div id="cart">
<form action="" method="post" name="shoppingcart" style="font-size:12px;">
    <?php 
    //We want to include the shopping cart in the email
    ob_start();
    ?>
    <table width="500" border="1" id="searchTbl">
      <tr>
        <th scope="col">Edit</th>
        <th scope="col">Description</th>
        <th scope="col">Quantity</th>
      </tr>

        <?php 
        //Print all the items in the shopping cart
        foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
        ?>
        <tr id="item<?php echo $itemNumber; ?>">    
            <td><a href="?remove=<?php echo $itemNumber; ?>">Löschen</a></td>
            <td><?php echo $item['name']; ?></td>
            <!-- <td><?php echo $item['price']; ?></td> -->
            <td><input name="items_qty[<?php echo $itemNumber; ?>]" type="text" id="item<?php echo $itemNumber; ?>_qty" value="<?php echo $item['qty']; ?>" size="2" maxlength="3" /></td>
            <!-- <td><?php echo $item['qty'] * $item['price']; ?></td>   -->      
        </tr>
        <?php
        }
        ?>
    </table>
    <?php $_SESSION['SHOPPING_CART_HTML'] = ob_get_flush(); ?>
    <p>
      <label>
      <input type="submit" name="update" id="update" value="Update Cart" />
      </label>
    </p>
</form>
<p><a href="?empty">Empty Cart</a></p>
</div>

Link for adding items to the cart looks as follows, the $description gets through correctly from a MySQL variable, so that works.

<a rel='facebox' href='editqty.php?name={$description}'><img src='img/mail.png' width='25' ></a>

editqty.php is a popup where you can enter the amount you want and click send, which then refers to the main.php file, which should get the values by doing $_GET:

<?php
try {
$id = $_GET['name'];
}

catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
?>


<form method="post" action="index.php">
<fieldset>
<legend>Amount?</legend>
<table>
<tr>
    <td>Amount:</td>
    <td><input type="text"  name="qty" required value="" autocomplete="off"></td>
    <td><input type="hidden" name="name" value="<?php echo $id; ?>" autocomplete="off"></td>
</tr>
<tr>
    <td></td>
    <td><input type="submit" name="add" value="Add"></td>
</tr>
</table>
</fieldset>
</form>

Now i know that the value for the name goes from the main.php per GET to the editqty.php, and there it is used in a hidden value and the qty value is added and these two then get sent back to the main.php, which works, i have tested that using tamper data, so i can see the two variables filled with infos getting through to main.php. Now the first script part in main.php actually seems to do something, it refers to the correct #cart and reloads the page, but there's no content then in that table that get's shown...

What i am doing wrong?

As always, thanks in advance, every hint is really appreciated!

Edit: I just found that if i change the URL to:

<a href='main.php?name={$description}&qty=5&add=foobar'><img src='img/mail.png' width='25' ></a>

It works like a charm, so i guess it has something to do that i am invoking another php script that does the add quantity for me, as i don't have an input for the quantity in the main.php i want to do that with an external php... Do i have to keep the session alive or something?

1

1 Answers

0
votes

I have found the Answer to my Question. I am going to leave this here for anybody else having this issue.

It was a basic mistake i made by using POST for the form to send my vars but the main.php was waiting for GET. So i just changed the form method to GET and it then worked.

Cheers