0

I'm trying to figure out why my PHP var does not update. I tried multiple solutions, not sure if i am doing something wrong, or this does just not work.

Any tips are appreciated.

<form method="post">
  <button type="submit" name="test"> 10  </button>
</form>

<?php
    if(!isset($_SESSION['money']))
    {
       $_SESSION['money'] = 100;
    }

    if(isset($_POST['test']))
    {
        $money = $_SESSION['money'];
        $money++;
        $_SESSION['money']= $money;
        echo $money;
    }
?>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Hendry
  • 87
  • 1
  • 9

2 Answers2

3

You don't seems to have started the session. Make sure you've put the line session_start(); at the top of your php files

Guillaume Sainthillier
  • 1,655
  • 1
  • 9
  • 13
1

missing session and action

<?php session_start();  ?>
<form method="post" action="">
  <button type="submit" name="test"> 10  </button>
</form>

<?php
    if(!isset($_SESSION['money']))
    {
       $_SESSION['money'] = 100;
    }

    if(isset($_POST['test']))
    {
        $money = $_SESSION['money'];
        $money++;
        $_SESSION['money']= $money;
        echo $money;
    }
?>
Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42