Jump to content

Help with Guess number game PHP


webdevdea

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.