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

时间:2016-01-28 12:20:11

标签: php forms

伙计这是我的主页

<?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>

这是我的页面,在用户点击他/她在每个问题上选择的所有单选按钮后检查easygame页面,它将带来多样性以验证他/她是否回答他/她是提交是对还是错。

<?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,我验证的答案是错误的?

1 个答案:

答案 0 :(得分:2)

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

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

<?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="answer" 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>