douhuan1901 2017-02-26 00:12
浏览 45
已采纳

PHP验证问题与/单独的无线电答案和粘性表格

I am trying to create a quiz where the user is answering multiple questions.

I made each question using radios and i am trying to check if that radio is checked. if the radio is not check then i display an error. I mostly got that part working.
My issues is that say if questions 1 has a radio that is checked and question 2 was not answered, when the user hits submit then the answer they put for question 1 is gone.

I want to keep the answer that they checked, like a sticky form, and only display the error on the questions, in this case question 2, that they did not answer. Below is two different ways i tried to solve it and i cant seem to accomplish it.

This is how i am displaying the question with 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>

This is how i am trying to verify it.

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;
    }
  • 写回答

1条回答 默认 最新

  • dongteng0748 2017-02-26 03:07
    关注

    When you submit a form with inputs using array notation [], they will come back as array in the $_POST. You would access your inputs using e.g. $_POST['q'][1].

    Just remember that $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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c