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
  • 33,530
  • 16
  • 73
  • 113
Hendry
  • 87
  • 1
  • 8

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,615
  • 1
  • 7
  • 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
  • 3,889
  • 3
  • 21
  • 40