webdevdea Posted March 16, 2014 Share Posted March 16, 2014 I have the game working but when it pulls up it automatically says the guess is to low try again What do I need to do for the game to pull up and start from scratch without assuming the user has already entered input? Here is the working link to see what I am talking abou t http://dandewebwonders.com/aliendata/phpguessing.php Here is my code <?php $guess=$_POST['guess']; $number= 25; if($guess>$number) { echo "Sorry, your guess is too high, try again"; echo "<form method=\"post\" name=\"guess\"> <input type=\"hidden\" name=\"number\" value=\"$number\"> Pick a number 1 through 50: <input name=\"guess\" type=\"text\"> <input name=\"submit\" type=\"submit\" value=\"Submit Guess\"> </form>"; }elseif($guess <= 15){ echo "Sorry, your guess is too low, try again"; echo "<form method=\"post\" name=\"guess\"> <input type=\"hidden\" name=\"number\" value=\"$number\"> Pick a number 1 through 50: <input name=\"guess\" type=\"text\"> <input name=\"submit\" type=\"submit\" value=\"Submit Guess\"> </form>"; }elseif($guess>15 && $guess<$number){ echo "You are getting Close!"; echo "<form method=\"post\" name=\"guess\"> <input type=\"hidden\" name=\"number\" value=\"$number\"> Pick a number 1 through 50: <input name=\"guess\" type=\"text\"> <input name=\"submit\" type=\"submit\" value=\"Submit Guess\"> </form>"; }elseif($guess==$number) { echo str_repeat("You got it, good guess", 25); echo "<form method=\"post\" name=\"guess\"> <input type=\"hidden\" name=\"number\" value=\"$number\"> Pick a number 1 through 50: <input name=\"guess\" type=\"text\"> <input name=\"submit\" type=\"submit\" value=\"Submit Guess\"> </form>"; } else{ ?><form method="post" name="guess"> <input type="hidden" name="number" value="<?php echo $number; ?>"> Pick a number 1 through 50: <input name="guess" type="text"> <input name="submit" type="submit" value="Submit Guess"> </form> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/287012-help-with-guess-number-game-php/ Share on other sites More sharing options...
Ch0cu3r Posted March 16, 2014 Share Posted March 16, 2014 You should only be checking the users guest when a post request has been made. Also you have a lot of repetitive code, you outputting the form 5 times, when all you need to do is echo it once. This the basic logic for your code should be like if(isset($_POST['submit'])) { // check users guess // echo message, too high, too low, getting close } // display form Link to comment https://forums.phpfreaks.com/topic/287012-help-with-guess-number-game-php/#findComment-1472788 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.