我正在猜測遊戲。猜測代碼正在工作,但是,當我想點擊'放棄'顯示數字時,不會將該值傳遞給放棄。我的道歉,我相當新的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>
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>
當你點擊giveup.php時,它只是包含一個表單...沒有任何東西被提交給guessinggame.php上的PHP處理代碼,所以你什麼都沒有得到。 – bear
你開始一個會話,但實際上並沒有使用它,使用會話來存儲變量而不是將它們存儲在表單中,這樣人們也無法通過查看錶單來作弊。 –
@shamil是的,這是真的,但是,任何暗示什麼樣的代碼可以考慮使用? –