watchnoface21 Posted October 10, 2010 Share Posted October 10, 2010 I want to write a guessing game that gives a user four tries to guess a number between 1 and 10. The problem is that every time the user hits submit, count is reset to zero and 1 is added. Please help. Here is my code: <html> <head> <title>Guessing Game </title> </head> <h1>Guessing Game </h1> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="hidden" name="count" value="<?php $count++; ?>" /> <p><input type="text" name="guess" /></p> <p><input type="submit" value="Guess" /></p> </form> </body> </html> <?php $random = rand(1,10); $guess = $_POST['guess']; if ($guess >= 10) { echo"Guess has to be lower than 10"; } elseif (isset($guess) && ($guess <= 1)) { echo "Guess has to be higher than 1"; } else { if ($guess == $random) { echo "Congratulations. You Win!!!!"; $count = 4; } elseif (isset($guess) && ($guess != $random)) { echo "Try again"; $count++; } elseif (($count == 4) && ($guess != $random)) { echo "Game Over. You Lose!!!"; } } ?> Link to comment https://forums.phpfreaks.com/topic/215549-updating-a-count-in-a-form/ Share on other sites More sharing options...
Pikachu2000 Posted October 10, 2010 Share Posted October 10, 2010 You'll probably want to store the value in a $_SESSION var, increment it with each try, then reset it to zero after the fourth attempt. Link to comment https://forums.phpfreaks.com/topic/215549-updating-a-count-in-a-form/#findComment-1120803 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.