从动态表单中添加/删除输入字段

时间:2021-01-10 14:06:44

标签: php forms

我正在用 PHP 创建一个测验站点,问题的第一个输入是通过上传 CSV,但我想提供在后端添加或删除问题和答案的可能性。 所有问题都存储在 questions 表中,与测验 id 相关,而答案在 answers 表中,包含问题 id、答案文本、以及一个布尔列,表示答案是否正确。

我无法理解如何添加或删除新问题和答案的输入字段(我是 php 和 js 的新手):您有什么建议吗?这是表格的代码,我可以在其中编辑问题和已经上传到数据库的答案:

<table>
    <?php
    try {
        $stmt = $db->prepare('SELECT * FROM questions WHERE id_quiz = :id_quiz ORDER BY id_qst');
        $stmt->execute(array(':id_quiz' => $_GET['id']));
        while($row = $stmt->fetch()){
            $id_qst=$row["id_qst"];
            $question = str_replace('"',"'",stripslashes($row['question']));

            echo "<tr>";
            echo '<td colspan="2"><input type="text" name="question_'.$id_qst.'" value="'.$question.'"></td>';
            echo "</tr>";

            $stmt1 = $db->prepare('SELECT * FROM answers WHERE id_qst = :id_qst ORDER BY id_rel_qa');
            $stmt1->execute(array(':id_qst' => $id_qst));
            $checked = "checked";
            
          while($row1 = $stmt1->fetch()){
          $id_rel_qa= $row1['id_rel_qa'];
            echo "<tr>";
            echo "<td colspan='2'>";

            echo '<input type="checkbox" value="1" name="correct_'.$id_rel_qa.'" class="checkbox"';
            if($row1['correct']==0){echo ' />';}else{echo 'checked />';}

            $a = str_replace('"',"'",stripslashes($row1['answer']));
            echo '<input type="text" name="a_'.$id_rel_qa.'" value="'.$a.'"></td>';

            echo "</td>";
            echo "</tr>";
        }
        }
    } catch(PDOException $e) {
            echo $e->getMessage();
            exit;
    }
    ?>
    </table>

0 个答案:

没有答案