【问题标题】:PHP get the values from input forms within a while loopPHP在while循环中从输入表单中获取值
【发布时间】:2017-07-19 14:44:37
【问题描述】:

你好,我有一个 PHP 代码,我在 while 循环中回显表单输入。我的代码的逻辑是我从表中选择问题,然后在它获取行时,每次它都会回显一个多选题测验的表格。所以现在如果表中有 20 个问题,我的代码将回显 20 个带有问题的表单。现在,当我按下提交按钮时,我想从每个表单中获取每个选定单选按钮的值,并检查我的表格的答案列。 这是我的代码:

<?php
include 'connection.php'; 

$query = "SELECT question,type,option1,option2,option3,option4,option5,option6,answer FROM question WHERE exam_id = '$exam_id'";
$result = mysqli_query($connection, $query);

while($row=mysqli_fetch_assoc($result))
{

    if ($row['type'] == "mcq") {
        echo '
      <form class="form-horizontal" role="form" method="POST" action="">
          <div class="form-group">

            <div class="col-sm-10">
            <p>'. $row["question"] . ' </p>

            </div>
          </div>
            <div id="form-label">
            <p class="alignleft"><b>Answer:</b></p>

              <div style="clear: both;"></div>
          </div>

          <!-- Text input-->
          <div class="form-group">
            <div class="col-md-2">
            A.  <input type="radio" placeholder="" name="answer" value = "a" id="" required> ' . $row["option1"].
           ' </div>

          </div>
          <!-- Text input-->
          <div class="form-group">
            <div class="col-md-2">
             B. <input type="radio" placeholder="" name="answer" value= "b" id=""> ' . $row["option2"].
            '</div>

          </div> 

           <!-- Text input-->
          <div class="form-group">
            <div class="col-md-2">
              C. <input type="radio" placeholder="" name="answer" value= "c" id=""> ' . $row["option3"].'
            </div>

          </div> 

        <!-- Text input-->
        <div class="form-group">
            <div class="col-md-2">
             D. <input type="radio" placeholder="" name="answer" value= "d" id=""> ' . $row["option4"].'
        </form>';
    }  
?>

我的问题

我不知道在哪里放置表单提交按钮以获取$_POST 值。如果我把它放在循环里面,它会打印每个问题,或者如果我把它放在循环外面,它就不起作用了,因为那时表单标签已经关闭了。

我想要做的每一个问题我想通过如果$_POST['answer'] == $row['answer'] 来检查答案,$_POST['answer'] 是单选按钮的表单值

【问题讨论】:

  • 什么没有工作;你有什么问题? $exam_id 来自哪里?您的问题不清楚。
  • 我不知道在哪里放置表单提交按钮以获取 $_POST 值。如果我把它放在循环里面,它会打印每个问题,或者如果我把它放在循环外面,它就不会工作,因为那时表单标签已经关闭了。我现在想要做的是对于我想要检查答案的每个问题,如果 $_POST['answer'] == $row['answer'] , $_POST['answer'] 是表单值单选按钮
  • 查看你的错误日志,你有一个未关闭的while循环开始,所以我猜这实际上并没有编译
  • 您好,欢迎来到 StackOverflow。请花一些时间阅读帮助页面,尤其是名为"What topics can I ask about here?""What types of questions should I avoid asking?" 的部分。更重要的是,请阅读the Stack Overflow question checklist。您可能还想了解Minimal, Complete, and Verifiable Examples

标签: php mysql


【解决方案1】:

这是一种使用计数器的解决方案,只有在打印完所有行后才在末尾添加提交标签

$count = 0;
while($row=mysqli_fetch_assoc($result))
{
 $count = $count + 1;

 if($count == Count($row)){
   Add submit button here
 }
}

【讨论】:

  • 我假设条件应该是 if($count == Count($row)){
猜你喜欢
  • 1970-01-01
  • 2021-04-14
  • 2019-07-01
  • 2021-10-19
  • 1970-01-01
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多