Hello,
I am trying to create a simple game random number guessing game in PHP.
Currently, I have an HTML page with a form on it where the user enters a number.
Then when the user clicks the submit button on the form, their number is passed onto the second page.
On the second page, I have some PHP code that generates a random number between 1 and 20, then it checks to see if the two numbers are the same.
If they are the same, it says correct.
If the guess is higher, it says so.
If the guess is lower, it says so.
I also made it display a form to re-guess the number (if the initial guess was incorrect).
The problem I have is when the user attempts to re-guess the number. Whenever the user attempts to enter a new guess, the random number changes.
I want to make it so that the random number stays the same as it is.
Any help?
Here is the Code for the first HTML page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>phpNumberGame</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="main"></div>
<div id="enterNumber">
<form action="result.php" method="post">
Enter a number: <input type="text" name="inputnumber" /><br />
<input type="submit" value="Guess" />
</form>
</div>
</body>
</html>
Here is the code for the second page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>result</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="main"></div>
<div id="enterNumber">
<?php
$number_actual = rand (1,20);
$number_guess = $_REQUEST["inputnumber"];
echo "You chose: " . $number_guess . "<br />";
echo "The random number is: " . $number_actual . "<br />";
if ($number_guess==$number_actual)
echo "You guessed <b>Correctly!</b>";
elseif ($number_guess<$number_actual)
{
echo "You are too <b>Low</b>!";
echo "<br />";
echo "<br />";
echo "Try guessing again...";
echo "<form>
<input type='text' name='inputnumber' />
<input type='submit' value='Guess' />
</form>";
}
elseif ($number_guess>$number_actual)
{
echo "You are too <b>High</b>!";
echo "<br />";
echo "<br />";
echo "Try guessing again...";
echo "<form>
<input type='text' name='inputnumber' />
<input type='submit' value='Guess' />
</form>";
}
?>
</div>
</body>
</html>
Here is what I have so far on my website, you can see it here:
http://joshkautz.com/phpNumberGame/phpNumberGame.html
I think I included everything necessary for people to give input, if not please let me know and I will give more information!
Do I need to redo things completely?
Any advice would be greatly appreciated.
Thanks a ton!