mbschoch Posted December 5, 2011 Share Posted December 5, 2011 Hello, I am learning php in a class for school and I was hoping I could get some help with some code. I am trying to make a game life counter. I just want it to have simple +1, -1 and reset buttons. I have gotten it to add and subtract the first time, but it will not continue adding/subtracting. Thank you for any help you can give. Here is the code I'm having issues with: <?php session_start(); ?> <form method="post" action =""> <input type="submit" name="name" value="Add"/> <input type="submit" name="name" value="Minus"/> <input type="submit" name="name" value="Reset"/> </form> <? lifeTotal(); function lifeTotal() { $_SESSION['total']=20; if($_POST["name"] == 'Add') { $_SESSION['total']++; echo $_SESSION['total']; } else if($_POST["name"] =='Minus') { $_SESSION['total']--; echo $_SESSION['total']; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252485-having-issues-with-code-for-simple-add-1subtract-1/ Share on other sites More sharing options...
KevinM1 Posted December 5, 2011 Share Posted December 5, 2011 The problem is that you keep resetting the total to 20. You need to check if a total exists. If there is NOT a pre-existing total, set it to 20. Else, simply use what you have for the calculation. Quote Link to comment https://forums.phpfreaks.com/topic/252485-having-issues-with-code-for-simple-add-1subtract-1/#findComment-1294490 Share on other sites More sharing options...
mbschoch Posted December 5, 2011 Author Share Posted December 5, 2011 Thank you! Got it! Quote Link to comment https://forums.phpfreaks.com/topic/252485-having-issues-with-code-for-simple-add-1subtract-1/#findComment-1294495 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.