Why does my page need to be refreshed to show the shopping cart update?

Posted on 2011-05-01
Last Modified: 2012-05-11
Hi, I have a function that returns the correct update:

function updateCart() {
      if((isset($_POST['update'])) && (isset($_POST['qty'])) && (isset($_POST['item']))) {
            $item = trim(mysql_clean_strings($_POST['item']));
            $qty = trim(mysql_clean_strings($_POST['qty']));
            $cart = $_SESSION['cart'];
            $_SESSION['cart'][$item] = $qty;
            return $qty;

but it doesn't show the update till the next time (either enter a new quantity or click refresh).  How can I make it show the changes on the first click (submit) ?

You will need to add something first:
Question by:FairyBusiness
    LVL 82

    Accepted Solution

    You need to do the update at the very start of the page. That way by the time you begin sending the "main" content for the page, $_SESSION will already reflect the updates.

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now