我是php的初学者。我正在做一个基本的数字猜游戏。有2个级别。level1.php
,我将用户输入发送到formprocess.php
echo "<form name = '"myfirstform'" action = '"formprocess.php'" method = '"POST'">";
echo "Enter Integer Between 1-5<br>";
echo "<input type = '"text'" name = '"firstdata'">";
echo "<br> <input type= '"submit'" value = '"submit'">";
echo "</form>";
在formprocess.php
我检查值是否正确
<?php
session_start();
if (!isset($_SESSION['wins']) || !isset($_SESSION['losses'])) {
$_SESSION['wins'] = 0;
$_SESSION['losses'] = 0;
}
$random = rand(1, 5);
if ($_POST["firstdata"] == $random){
$_SESSION['wins']++;
echo "<h1><font color='"green'">Congrulations!</h1></font><br>";
else{
$_SESSION['losses']++;
echo "<h1><font color='"red'">Nope wrong answer</h1></font><br>";
它工作得很好(我跳过了其他代码)现在我想将玩家发送到 level2.php如果他的答案是正确的。我尝试使用这样的隐藏输入来做到这一点:
echo "<form name = '"mysecondform'" action = '"level2.php'" method = '"POST'">";
echo " <input type='"hidden'" name='"seconddata'" value='"1'">";
echo "<br> <input type= '"submit'" value = '"Go to Level2'">";
level2.php
我的代码是:
<?php
if ($_POST["seconddata"] == 1){
echo "<html><head><title>Calculator Game From 1998</title></head><body>";
echo "<h1>Please Guess The Answer-Level2</h1>";
echo "<form name = '"myfirstform'" action = '"formprocess.php'" method = '"POST'">";
echo "Enter Integer Between 1-10<br>";
echo "<input type = '"text'" name = '"firstdata'">";
echo "<br> <input type= '"submit'" value = '"submit'">";
echo "</form>";
echo "</body></html>";
}
else{
echo "You didn't finish level1"
}
?>
即使答案正确,此页面中也没有任何内容。我更改了1
值,"1"
不再工作。那里有什么问题
如果您的页面显示为空白可能是因为 php 错误,请将下一个代码放在 php 脚本的顶部
error_reporting(E_ALL);
ini_set("display_errors", 1);
愚蠢的错误
我应该写
echo "You didn't finish level1"
而不是
else{
echo "You didn't finish level1"
}