Hi All,

So I an creating an online store just to play around with code and learn from it. I am building a store and on the confirmation page, I am trying to update the QTY of items in a cart by using the '-' or '+' buttons. However I am having no luck at all.

Here is my code:

<?php       
    foreach ($_SESSION["cart_item"] as $item){
        $item_price = $item["quantity"]*$item["price"];
        ?>
                <tr>
                <td><img src="<?php echo $item["image"]; ?>" class="cart-item-image" /><?php echo $item["name"]; ?></td>
                <td><?php echo $item["code"]; ?></td>
                <td style="text-align:center;">

                <table class ="cart-info">
                <tbody>
                <tr>
                    <form method='post'>
                    <td>
                        <button class="btn-increment-decrement" id="add" onClick="decrement_quantity(<?php echo $_SESSION['quantity']-- ?>, <?php echo $_SESSION['price']; ?>)">-</button>
                    </td>
                    <td>
                    <input class="input-quantity" id="input-quantity-<?php echo $item["cart_id"]; ?>" value="<?php echo $item["quantity"]; ?>"></td>
                    <td>
                    <button class="btn-increment-decrement"
                        onClick="increment_quantity(<?php echo $_SESSION['quantity']++; ?>, '<?php echo $_SESSION['price']; ?>')">+</button>
                    </td>
                        </form>
                </tr>
                </tbody>
                </table>   

                </td>
                <td  style="text-align:right;"><?php echo "£ ".$item["price"]; ?></td>
                <td  style="text-align:right;"><?php echo "£ ". number_format($item_price,2); ?></td>
                <td style="text-align:center;"><a href="?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction"><img src="images/icons/delete.png" alt="Remove Item" /></a></td>
                </tr>
                <?php
                $total_quantity += $item["quantity"];
                $total_price += ($item["price"]*$item["quantity"]);
        }
        ?>

Here is the UI Click Here

Thanks

Here is an example showing the essential moving parts, HTH

<?php
/**
 * Clickable Buttons to Adjust a Counting Value in the PHP session
 */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('log_errors', TRUE);

/**
 * Always start the PHP session on every page.
 *
 * Since it is a required feature of this code example, we test to see that the
 * call to session_start() worked, and terminate the script if it failed.
 */
if (!session_start()) trigger_error("Unable to start the PHP session", E_USER_ERROR);

/**
 * On the first request, the counter will not exist, so we create it, set to zero
 */
if (!isset($_SESSION['counter'])) $_SESSION['counter'] = 0;

/**
 * Look for POST-method requests to increment or decrement our session variable
 */
if (!empty($_POST['add'])) {
    $_SESSION['counter']++;
}
if (!empty($_POST['sub'])) {
    $_SESSION['counter']--;
}

/**
 * Create an HTML form to communicate with this script using HEREDOC notation
 *
 * Unfortunately you cannot use an expression like $_SESSION['count'] in the
 * HEREDOC block, so we are forced to create a separate variable that can be
 * injected into the HEREDOC.  Not a problem with object-oriented notation.
 * Maybe some day PHP will allow us to create the session in the form of an
 * object instead of an array!
 */
$session_counter = $_SESSION['counter'];
$form = <<<ENDFORM
The value of the session counter is $session_counter
<form method="post">
<input type="submit" name="add" value="+1" />
<input type="submit" name="sub" value="-1" />
</form>
ENDFORM;

echo $form;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.