Hi Guys, I was wondering how to make this script work. I want it to add 1 to a variable every time the button is clicked. I already have, <?php session_start(); // if your variable is not yet defined, you assigned it with 0 if (isset($_SESSION['count'])) { $_SESSION['count'] = 0; } if($_POST['submit']) { $_SESSION['count'] = $_SESSION['count'] + 1; echo $_SESSION['count']; ++$_SESSION['count']; } ?> <!DOCTYPE html> <html> <head> <title>Bale Counter</title> </head> <body> <form action="index.php" method="post"> <input type="submit" name="submit" value="Bale Out"> </form> </body> </html> PHP: How do I make this work?
Hi. Try the following modified code: <?php session_start(); // if your variable is not yet defined, you assigned it with 0 if (!isset($_SESSION['count'])) { $_SESSION['count'] = 0; } if(isset($_POST['submit'])) { $_SESSION['count']++; echo $_SESSION['count']; } ?> <!DOCTYPE html> <html> <head> <title>Bale Counter</title> </head> <body> <form action="index.php" method="post"> <input type="submit" name="submit" value="Bale Out"> </form> </body> </html> PHP:
Do you want to save that value for future reference as wel or just want that for a particular session?
I hope you know that session variable will expire within 30 to 180 mins depending on your PHP installation.
You should replace your following code if (isset($_SESSION['count'])) { $_SESSION['count'] = 0; } with if (!isset($_SESSION['count'])) { $_SESSION['count'] = 0; }