0

我正在做一个猜谜游戏。猜测代码正在工作,但是,当我想单击“放弃”以显示数字时,并没有将值传递给放弃。抱歉,我对 php 很陌生。

任何建议或提示如何做到这一点?

下面是guessinggame.php,底部是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>

放弃.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>
4

1 回答 1

2

您可以将数字存储在主脚本的用户会话中:

session_start();
$number = rand(1,100);
$_SESSION['number'] = $number;

然后,检索它giveup.php

$number = $_SESSION['number'];
于 2013-10-29T23:34:34.170 回答