我有一个猜谜游戏的php脚本和一个祝贺页面的html脚本。如果猜测是正确的,游戏将结束并且祝贺页面将打开。在PHP中,我有一个变量$ prize = 1000-100 * $ _POST ['tries'],这样如果第一个猜测是正确的,玩家将赢得$ 1000;如果玩家有第二次猜测,奖品将少于100美元,依此类推。这个变量作为$ _POST ['prize']保存在php的隐藏字段中。我希望最终的奖品可以印在祝贺页面上,但它没有像我期望的那样工作。我在HTML中做了什么错误?谢谢你们,玛丽亚。祝贺页面没有显示猜数游戏中的变量
guess.php:
<?php
if(isset($_POST['number'])) {
$num = $_POST['number'];
} else {
$num = rand(1,10);
}
if(isset($_POST['prize'])) {
$prize =1000-100 * $_POST['tries'];
} else {
$prize = 900;
}
$tries=(isset($_POST['guess'])) ? $_POST['tries']+1: 0;
if (!isset($_POST['guess'])) {
$message="Welcome to the Guessing Game!";
} elseif (!is_numeric($_POST['guess'])) {
$message="You need to type in a number.";
} elseif ($_POST['guess']==$num) {
header("Location: Congrats.html");
exit;
} elseif ($_POST['guess']>$num) {
$message="Try a smaller number";
} else {
$message="Try a bigger number";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Guessing Game</title>
</head>
<body>
<h1><?php echo $message; ?></h1>
<p><strong>Guess number: </strong><?php echo $tries; ?></p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p><label for="guess">Type your guess here:</label><br/>
<input type="text" id="guess" name="guess" />
<input type="hidden" name="tries" value="<?php echo $tries; ?>"/><br/>
<input type="hidden" name="number" value="<?php echo $num; ?>"/><br/>
<input type="hidden" name="prize" value="<?php echo $prize; ?>"/>
</p>
<button type="submit" name="submit" value="submit">Submit</button>
</form>
</body>
</html>
congrats.html:
<! DOCTYPE html>
<html>
<header>
<title>Congratulation!</title>
<body>Congratulation!<br/>
You Won <?php echo $_POST['prize']; ?> dollars!
</body>
</header>
</html>