PHP如何实现提交表单参数后跳转页面展示对应知识点及试题
需求背景
- 页面文件:
setTearmlyExam.php、setTopics.php - 数据表:试题存储表
olevelguestion、知识点存储表Topics - 核心需求:用户在
setTearmlyExam.php完成表单选择提交后,跳转至setTopics.php,基于提交的学期、科目、考试类型参数,查询并展示所有匹配的知识点及对应试题。
现有代码
setTearmlyExam.php
<form action="setTopics.php " method="POST" class="col s12" center> <!-- setTopics.php --> <div> <label>Select Term</label> <select name="terms" class="browser-default" required> <option value="" disabled selected>Select Term</option> <?php // Selecting terms from database $select_terms = $db->query("SELECT * FROM term"); if (!$select_terms) { die('QUERY FAILED' .mysqli_error($db)); } // Using while loop to fatch and loop term titles while ($row = $select_terms->fetch_assoc()) { $termId = $row['termId']; $terms = $row['terms']; echo "<option value='{$termId}'>{$terms}</option>"; } ?> </select> <label>Select Subject</label> <select name="subjects" class="browser-default" required> <option value="" disabled selected>Select Subject</option> <?php // Selecting subjects from database $Select_subjects = $db->query("SELECT * FROM subjects ORDER BY subj ASC"); if (!$Select_subjects) { die("QUERY FAILED" . mysqli_error($db)); } // Using while loop to fetch the subjects title while ($rw = $Select_subjects->fetch_assoc()) { $subjId = $rw['subjId']; $subj = $rw['subj']; echo "<option value='{$subjId}'>{$subj}</option>"; } ?> </select> </div> <div class="ST"> <div class="col 6"> <label> <input class="with-gap" name="Examtype" value="O" type="radio" required/> <span>Objective</span> </label> </div> <div class="col 12"> <label> <input class="with-gap" name="Examtype" value="T" type="radio" /> <span>Theory</span> </label> </div> </div> </div> </div> <div class="card-action centerTxt"> <button class="btn waves-effect waves-light " type="submit" name="start">Start <i class="material-icons right">send</i> </button> </div> </div> </form>
原有setTopics.php代码
<form action=""> <div class="card-content black-text"> <span class="card-title centerTxt"> <h4>Subject Title</h4> </span> <div> <!-- Dropdown Trigger --> <label for=""> <h6 class="" style="font-size: 15px; color:black;">CLICK ON EACH TOPIC TO SELECT QUESTIONS. <span style="color:red; font-size: 13px;">Don't Select more than 50 Questions.</span></h6> <?php $terms = 'terms'; $subjects = 'subjects'; // $Examtype = $_GET['Examtype']; if ($Examtype = 'O') { $topics = $db->query('SELECT * FROM topics WHERE '); } ?> <h5 class='dropdown-trigger ' style="color:#149fd5;" data-target='dropdown1'>Topics Here</h5> </label> <!-- Dropdown Structure --> <ul id='dropdown1' class='dropdown-content'> <p> <label> <input type="checkbox" value="" /> <span>Yellow</span> </label> </p> </ul> </div> </div> <div class="card-action centerTxt"> <button class="btn waves-effect waves-light " type="submit" name="start">Next <i class="material-icons right">send</i> </button> </div> </form>
实现方案
核心逻辑说明
setTearmlyExam.php表单以POST方式提交参数,setTopics.php接收参数后先做合法性校验,再关联查询两个表获取匹配数据,最后渲染到页面。
修改后的setTopics.php代码示例
<?php // 需保证数据库连接实例$db已提前初始化 // 校验POST参数是否完整 if (!isset($_POST['terms'], $_POST['subjects'], $_POST['Examtype'])) { // 参数缺失直接跳转回选择页 header('Location: setTearmlyExam.php'); exit; } // 接收并格式化参数 $term_id = intval($_POST['terms']); $subj_id = intval($_POST['subjects']); $exam_type = trim($_POST['Examtype']); // 查询科目名称用于页面展示 $subj_stmt = $db->prepare("SELECT subj FROM subjects WHERE subjId = ?"); $subj_stmt->bind_param("i", $subj_id); $subj_stmt->execute(); $subj_res = $subj_stmt->get_result(); $subj_name = $subj_res->fetch_assoc()['subj'] ?? '未知科目'; // 查询匹配当前学期、科目的知识点列表 需根据实际表字段调整关联条件 $topic_stmt = $db->prepare("SELECT * FROM Topics WHERE subjId = ? AND termId = ?"); $topic_stmt->bind_param("ii", $subj_id, $term_id); $topic_stmt->execute(); $topics_result = $topic_stmt->get_result(); ?> <form action="后续处理逻辑的文件地址.php" method="POST"> <div class="card-content black-text"> <span class="card-title centerTxt"> <h4><?php echo htmlspecialchars($subj_name); ?></h4> </span> <div> <label for=""> <h6 class="" style="font-size: 15px; color:black;">点击对应知识点选择试题 <span style="color:red; font-size: 13px;">选题总数不得超过50道</span></h6> <h5 class='dropdown-trigger ' style="color:#149fd5;" data-target='dropdown1'>知识点列表</h5> </label> <ul id='dropdown1' class='dropdown-content'> <?php while ($topic = $topics_result->fetch_assoc()): // 查询当前知识点下匹配考试类型的试题数量 $ques_stmt = $db->prepare("SELECT * FROM olevelguestion WHERE topicId = ? AND examType = ? AND subjId = ? AND termId = ?"); $ques_stmt->bind_param("isii", $topic['topicId'], $exam_type, $subj_id, $term_id); $ques_stmt->execute(); $ques_count = $ques_stmt->get_result()->num_rows; ?> <li> <p> <label> <input type="checkbox" name="selected_topics[]" value="<?php echo $topic['topicId']; ?>" /> <span><?php echo htmlspecialchars($topic['topicName']) . "({$ques_count}道题)"; ?></span> </label> </p> </li> <?php endwhile; ?> </ul> </div> </div> <div class="card-action centerTxt"> <!-- 隐藏域传递基础参数,供后续逻辑使用 --> <input type="hidden" name="termId" value="<?php echo $term_id; ?>"> <input type="hidden" name="subjId" value="<?php echo $subj_id; ?>"> <input type="hidden" name="examType" value="<?php echo htmlspecialchars($exam_type); ?>"> <button class="btn waves-effect waves-light " type="submit" name="start">下一步 <i class="material-icons right">send</i> </button> </div> </form>
注意事项
- 代码中关联查询的字段需和实际表结构对齐,若字段名不同可自行调整
- 所有输出到页面的内容均做了
htmlspecialchars转义,避免XSS漏洞 - 数据库查询使用预处理语句,避免SQL注入风险
- 异常参数访问会自动跳转回参数选择页,减少业务异常
内容的提问来源于stack exchange,提问作者Joachim Chisom
相关产品推荐
相关产品推荐

