There is a simple form using AJAX - 2 text fields and a button to send values:

 <form method="post" name="chooseProduct" class="choose-product-form"> <input type="text" name="product" placeholder="Название продукта"> <input type="text" name="portion" placeholder="Размер порции"> <input type="button" value="Добавить" id="addProduct"> </form> 

Each time you press the button, the values ​​(float) are added to the previous ones and all this is saved to the session variable.

Can PHP clear the session variable when the page is updated (reloaded) using PHP ?

  • one
    unset($_SESSION['someValue']) ? - Vasily Barbashev
  • What question, such an answer: is possible. - Gino Pane

1 answer 1

A couple of options:

  1. By default, clear the value of the session variable. Add a flag parameter to your Ajax request telling you not to clear the variable.

  2. Add a php script to the page load event to clear the session variable.

 if(isset($_REQUEST["cleanup"])){ if(isset($_SESSION["sum"])) $_SESSION["sum"] = 0; }