如何将上一页的随机值调用到另一页


how do i call the previous page's random value to another page?

伙计们,这是我的主页

<?php
$random = rand(4, 5);
$random2 = rand(1, 12);
$random3 = rand(1, 60);
$random4 = rand(1, 60);
$random5 = rand(1, 60);
if ($random2 % 4 != 0) {
$random2 += 4 - ($random2 % 4);
$call = $random;
} else {
$random2 += 4 - ($random2 % 4);
$call = $random2;
}
$answer = $random * $random2;
?>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="stylesheet" href="CSS/main.css" type="text/css"/>
    <link rel="stylesheet" href="CSS/easy2.css" type="text/css"/>
    <title> Easy Game 2 </title>
<center>
    <h1> Easy Game 2 </h1>
    <h2> Multiple Choice! </h2>
    <div class="border_solid">
        <div id="timer"></div>
    </div>
    <hr>
</center>
</head>
<body>
<form method="post" action="doeasygame2.php">
    <table cellspacing="40" class="table">
        <tr>
            <td>
                <label for="question_1"><?php echo $random; ?> X <?php echo $random2; ?> =?</label>
                <input type="radio" id="mcq" name="mcq" value="<?php echo $random3; ?>"><?php echo $random3; ?>
                <input type="radio" id="mcq" name="mcq" value="<?php echo $random4; ?>"><?php echo $random4; ?>
                <input type="radio" id="mcq" name="mcq" value="<?php echo $random5; ?>"><?php echo $random5; ?>
                <input type="radio" id="mcq" name="mcq" value="<?php echo $answer; ?>"><?php echo $answer; ?>
            </td>
            <td>
                <input type="submit" class="submit" value="submit" name="submit"/><br>
            </td>
        </tr>
    </table>
</form>
</body>
<script type="text/javascript">
var myVar = setInterval(function () {
    myTimer()
}, 1000);
var d = 0;
function myTimer() {
    document.getElementById("timer").innerHTML = d++;
}
</script>
</html>

这是我的DO页面,用于在用户单击他/她在每个问题上选择的所有单选按钮后检查EasyGame页面,它将带到Dopage以验证他/她提交的答案是否正确。

<?php
$random = isset($_POST['random']) ? intval($_POST['random']) : 0;
$random2 = isset($_POST['random2']) ? intval($_POST['random2']) : 0;
$random3 = isset($_POST['random3'])? intval($_POST['random3']) : 0;
$random4 = isset($_POST['random4'])? intval($_POST['random4']) : 0;
$random5 = isset($_POST['random5'])? intval($_POST['random5']) : 0;
$answer = isset($_POST['answer'])? intval($_POST['answer']) : 0;
$mcq = $_POST['mcq'];
?>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="stylesheet" href="CSS/main.css" type="text/css"/>
    <link rel="stylesheet" href="CSS/easy2.css" type="text/css"/>
    <title> Easy Game 2 </title>
<center>
    <h1> Easy Game 2 </h1>
    <h2> Multiple Choice! </h2>
    <hr>
</center>
</head>
<body>
<table cellspacing="40" class="table">
    <tr>
        <td>
            <label for="question_1"><?php echo $random; ?> X <?php echo $random2; ?> =?</label>
            <input type="radio" id="mcq" name="mcq" value="<?php echo $random3; ?>"><?php echo $random3; ?>
            <input type="radio" id="mcq" name="mcq" value="<?php echo $random4; ?>"><?php echo $random4; ?>
            <input type="radio" id="mcq" name="mcq" value="<?php echo $random5; ?>"><?php echo $random5; ?>
            <input type="radio" id="mcq" name="mcq"><?php echo $answer; ?>
            <?php
            if ($mcq == $answer) {
                echo "Correct!";
            } else {
                echo "Wrong!";
            }
            ?>
        </td>
    </tr>
</table>
</body>
</html>

为什么当我单击第一页的单选按钮并提交时,下一页上的值为 0,而我验证的答案是错误的?

我查看了您的代码,发现您没有在第一页存储选项,当您尝试将其发布到下一页时,它将变为零,因为所有单选按钮都具有相同的名称,当您将其发布到下一页时,它只会发布选定的无线电值。

为了满足您的要求,您只需要在下面使用我的代码作为第一页:

<?php
$random = rand(4, 5);
$random2 = rand(1, 12);
$random3 = rand(1, 60);
$random4 = rand(1, 60);
$random5 = rand(1, 60);
if ($random2 % 4 != 0) {
$random2 += 4 - ($random2 % 4);
$call = $random;
} else {
$random2 += 4 - ($random2 % 4);
$call = $random2;
}
$answer = $random * $random2;
?>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="stylesheet" href="CSS/main.css" type="text/css"/>
    <link rel="stylesheet" href="CSS/easy2.css" type="text/css"/>
    <title> Easy Game 2 </title>
<center>
    <h1> Easy Game 2 </h1>
    <h2> Multiple Choice! </h2>
    <div class="border_solid">
        <div id="timer"></div>
    </div>
    <hr>
</center>
</head>
<body>
<form method="post" action="doeasygame2.php">
<table cellspacing="40" class="table">
        <tr>
            <td>
                <label for="question_1"><?php echo $random; ?> X <?php echo $random2; ?> =?</label>
                <input type="radio" id="mcq" name="mcq" value="<?php echo $random3; ?>"><?php echo $random3; ?>
                <input type="radio" id="mcq" name="mcq" value="<?php echo $random4; ?>"><?php echo $random4; ?>
                <input type="radio" id="mcq" name="mcq" value="<?php echo $random5; ?>"><?php echo $random5; ?>
                <input type="radio" id="mcq" name="mcq" value="<?php echo $answer; ?>"><?php echo $answer; ?>
            </td>
            <td>
                <input type="submit" class="submit" value="submit" name="submit"/><br>
            </td>
        </tr>
    </table>
    <input type="hidden" name="random" value="<?php print $random ?>">
    <input type="hidden" name="random2" value="<?php print $random2 ?>">
    <input type="hidden" name="random3" value="<?php print $random3 ?>">
    <input type="hidden" name="random4" value="<?php print $random4 ?>">
    <input type="hidden" name="random5" value="<?php print $random5 ?>">
    <input type="hidden" name="answerswer" value="<?php print $answer ?>">
</form>
</body>
<script type="text/javascript">
var myVar = setInterval(function () {
    myTimer()
}, 1000);
var d = 0;
function myTimer() {
    document.getElementById("timer").innerHTML = d++;
}
</script>
</html>

请在第二页使用以下代码:

<?php
$random = isset($_POST['random']) ? intval($_POST['random']) : 0;
$random2 = isset($_POST['random2']) ? intval($_POST['random2']) : 0;
$random3 = isset($_POST['random3'])? intval($_POST['random3']) : 0;
$random4 = isset($_POST['random4'])? intval($_POST['random4']) : 0;
$random5 = isset($_POST['random5'])? intval($_POST['random5']) : 0;
$answer = isset($_POST['answer'])? intval($_POST['answer']) : 0;
$mcq = $_POST['mcq'];
?>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="stylesheet" href="CSS/main.css" type="text/css"/>
    <link rel="stylesheet" href="CSS/easy2.css" type="text/css"/>
    <title> Easy Game 2 </title>
<center>
    <h1> Easy Game 2 </h1>
    <h2> Multiple Choice! </h2>
    <hr>
</center>
</head>
<body>
<table cellspacing="40" class="table">
    <tr>
        <td>
            <label for="question_1"><?php echo $random; ?> X <?php echo $random2; ?> =?</label>
            <input type="radio" id="mcq" name="mcq" value="<?php echo $random3; ?>" <?php print $random3 == $mcq ? 'checked="checked"' : '' ?> ><?php echo $random3; ?>
            <input type="radio" id="mcq" name="mcq" value="<?php echo $random4; ?>" <?php print $random4 == $mcq ? 'checked="checked"' : '' ?> ><?php echo $random4; ?>
            <input type="radio" id="mcq" name="mcq" value="<?php echo $random5; ?>" <?php print $random5 == $mcq ? 'checked="checked"' : '' ?> ><?php echo $random5; ?>
            <input type="radio" id="mcq" name="mcq" <?php print $mcq == $answer ? 'checked="checked"' : '' ?> ><?php echo $answer; ?>
            <?php
            if ($mcq == $answer) {
                echo "Correct!";
            } else {
                echo "Wrong!";
            }
            ?>
        </td>
    </tr>
</table>
</body>
</html>