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
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
Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.