1

When a button is clicked my counter should be lowered by 1 or 2 depending on the clicked button. This works the first time but when a button is clicked again the original value of my counter is used to make this second calculation rather then the result of the previous calculation. I don't understand why this new value is not being saved.

This is my code;

<?php // luciferSpel.php declare(strict_types=1); session_start(); if(!isset($_SESSION["lucifer"])) { $_SESSION["lucifers"] = array_fill(0,7,"lucifer"); $_SESSION["teller"] = 6; } if (isset($_GET['reset']) && $_GET['reset'] == 1) { unset($_SESSION["lucifers"]); unset($_SESSION["teller"]); header("Location: " .$_SERVER["PHP_SELF"]); exit; } $lucifers = &$_SESSION["lucifers"]; $teller = &$_SESSION["teller"]; if (isset($_POST["min1"]) && $teller >= 0) { unset($lucifers[$teller]); $teller --; } if (isset($_POST["min2"]) && $teller >=1) { unset($lucifers[$teller],$lucifers[$teller-1]); $teller -= 2; } if ($teller === -1) { header("Location: luciferSpelAfgelopen.php "); exit; } ?> <!DOCTYPE HTML> <html> <head> <meta charset=utf-8> <title>Luciferspel</title> </head> <body> <h1>Luciferspel</h1> <?php echo $teller; echo count($lucifers) ?> <?php for ($f=0; $f < count($lucifers); $f++) :?> <img src="img/lucifer.png" alt="lucifer" /> <?php endfor; ?> <form method="post"> <button type="submit" name="min1">Een lucifer wegnemen </button> <button type="submit" name="min2">Twee lucifers wegnemen </button> </form> <p>Klik <a href="?reset=1" >hier</a> om een nieuw spel te starten.</p> </body> </html> 

I tried several variation on this code where the counter is saved differently, b.e. after every calculation (&$_SESSION["teller"]=$teller). But the result of the calculation is never saved to start the next calculation with. It always resets to the original setting of 6.

8
  • Do you have session_start() in the code someplace? You need to open the session before you can use a session variable. Also, I'm confused why you're using <a href="?reset=1" > when you don't use the $_POST key 'reset' at all. You also don't seem to use the $_POST key 'min1' at all, either. This code looks like a culmination of several different efforts to achieve the same goal, but there are remnants of past efforts in place. Is that what's happened? If so, cleaning up the code will help us make sense of it. If not, please explain where these tokens are used. Commented Jul 17, 2024 at 19:53
  • @Kaboodleschmitt URL query parameters are in $_GET, not $_POST. They have if (isset($_GET['reset']) && $_GET['reset'] == 1) Commented Jul 17, 2024 at 20:13
  • @Kaboodleschmitt Why do you say they don't use min1? Don't you see if (isset($_POST["min1"]) && $teller >= 0)? Commented Jul 17, 2024 at 20:14
  • @Kaboodleschmitt session_start() is on line 6. Did you actually read the code? Commented Jul 17, 2024 at 20:15
  • 1
    Typo here: if(!isset($_SESSION["lucifer"])) you left out the s in lucifers. Commented Jul 17, 2024 at 20:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.