Reputation: 67
I am working on a guessing game. The guessing code is working, however, when i want to click the 'give up' to display the number, is not passing the value to the give up. My apology, I am fairly new with php.
Any suggestion or hint how this could be done?
below is the guessinggame.php and the bottom one is the giveup.php
<?php
session_start();
$number = rand(1,100);
if(isset($_POST["guess"])){
$guess = $_POST['guess'];
$number = $_POST['number'];
$display = $_POST['submit'];
if ($guess < $number){
echo "The number needs to be higer!";
}else
if($guess > $number){
echo "The number needs to be lower!";
}else
if($guess == $number){
echo "Congratulation! You Guessed the hidden number.";
}
}
?>
<!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>Guess A Number</title>
</head>
<body>
<form action="<?=$_SERVER['PHP_SELF'] ?>" method="post" name="guess-a-number">
<label for="guess"><h1>Guess a Number:</h1></label><br/ >
<input type="text" name="guess" />
<input name="number" type="hidden" value="<?= $number ?>" />
<input name="submit" type="submit" />
<br/ >
<a href="giveup.php">Give Up</a>
<br/ >
<a href="startover.php">Start Over</a>
</form>
</body>
</html>
giveup.php
<!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>Guess A Number</title>
</head>
<body>
<form action="guessinggame.php" method="GET" name="guess-a-number">
<?php echo "<br />The hidden number is:".$number."<br />";?>
<br/ >
<a href="startover.php">Start Over</a>
</form>
</body>
</html>
Upvotes: 0
Views: 128
Reputation: 19539
You could store the number in the user session in your main script:
session_start();
$number = rand(1,100);
$_SESSION['number'] = $number;
Then, retrieve it in giveup.php
:
$number = $_SESSION['number'];
Upvotes: 2