0

我正在尝试创建一个用户回答多个问题的测验。

我使用收音机提出了每个问题,我正在尝试检查是否检查了该收音机。如果收音机没有被检查,那么我会显示一个错误。我大部分时间都在工作。
我的问题是,如果问题 1 有一个已检查的收音机并且问题 2 没有得到回答,那么当用户点击提交时,他们为问题 1 输入的答案就消失了。

我想保留他们检查过的答案,就像一个粘性表格一样,并且只在问题上显示错误,在这种情况下是问题 2,他们没有回答。以下是我尝试解决它的两种不同方法,但我似乎无法完成它。

这就是我用 php 显示问题的方式。

<tr>
    <td> If a and b are negative numbers, and |a| < |b|, then b - a is negative. </td>
    <td> <?php echo ($err['q[1]']? "<span style='color:red'>*".$err['q[1]']."</span><br>": "");?>
        <input type="radio" name="q[1]" id="q[1]t" value="T"  <?php if (isset($_POST['q[1]']) and $_POST['q[1]'] == 'T') { echo 'checked'; } ?> > TRUE
        <input type="radio" name="q[1]" id="q[1]f" value="F"  <?php if (isset($_POST['q[1]']) and $_POST['q[1]'] == 'F') echo 'checked'; ?> > FALSE
    </td>
</tr>
<tr>
    <td> The equation 2x + 7 = 2(x + 5) has one solution. </td>
    <td>    <?php echo ($err['q[2]']? "<span style='color:red'>*".$err['q[2]']."</span><br>": "");?>
        <input type="radio" name="q[2]" id="q[2]t" value="T"  <?php if (isset($_POST['q[2]']) and $_POST['q[2]'] == 'T') echo 'checked'; ?> > TRUE
        <input type="radio" name="q[2]" id="q[2]f" value="F"  <?php if (isset($_POST['q[2]']) and $_POST['q[2]'] == 'F') echo 'checked'; ?> > FALSE
    </td>
</tr>

这就是我试图验证它的方式。

if (isset($_POST) && !empty($_POST)) {

    if (isset($_POST['q[1]'])) {
        $radio_input = $_POST['q[1]'];
        echo $radio_input;
        $error=false;
    } else {
        $err['q[1]']= "Please Select An Answer";
        $error=true;
    }
    if (empty($_POST['q[2]'])) {
        $err['q[2]']= "Please Select An Answer";
        $error=true;
    } else {
        $error=false;
    }
4

1 回答 1

1

当您使用数组表示法提交带有输入的表单时[],它们将作为数组返回$_POST. 您将使用例如访问您的输入$_POST['q'][1]

只要记住这一点$err['q[1]'] !== $err['q'][1]

<?php
$err = array();
// check if form was submitted with POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // simplified
    for ($i = 1; $i <= 2; $i++) {
        // check if no answer was selected
        if (empty($_POST['q'][$i])) {
            $err["q[$i]"] = "Please Select An Answer";
        }
    }
}
?>

<form action="" method="post">
    <table>
        <tr>
            <td> If a and b are negative numbers, and |a| < |b|, then b - a is negative.</td>
            <td> 
                <?php if (isset($err['q[1]'])) : ?>
                    <span style="color: red">* <?= $err['q[1]'] ?></span><br>
                <?php endif ?>
                <input type="radio" name="q[1]" id="q[1]t" value="T" <?= isset($_POST['q'][1]) && $_POST['q'][1] == 'T' ? 'checked' : '' ?>> TRUE
                <input type="radio" name="q[1]" id="q[1]f" value="F" <?= isset($_POST['q'][1]) && $_POST['q'][1] == 'F' ? 'checked' : '' ?>> FALSE
            </td>
        </tr>
        <tr>
            <td> The equation 2x + 7 = 2(x + 5) has one solution. </td>
            <td>    
                <?php if (isset($err['q[2]'])) : ?>
                    <span style="color: red">* <?= $err['q[2]'] ?></span><br>
                <?php endif ?>
                <input type="radio" name="q[2]" id="q[2]t" value="T" <?= isset($_POST['q'][2]) && $_POST['q'][2] == 'T' ? 'checked' : '' ?>> TRUE
                <input type="radio" name="q[2]" id="q[2]f" value="F" <?= isset($_POST['q'][2]) && $_POST['q'][2] == 'F' ? 'checked' : '' ?>> FALSE
            </td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Check"></td>
        </tr>
    </table>
</form>
于 2017-02-26T03:07:32.533 回答