Php从在数据库中获取的循环内运行的数据中获取

时间:2017-07-19 09:19:03

标签: php html mysql

您好我有一个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'] == "true/false") {
     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>Mark this question as:</b></p>

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

          <!-- Text input-->
          <div class="form-group">
            <div class="col-md-2">
              <input type="radio" placeholder="" name="answer" value = "true" id="" required> True
            </div>

          </div>


          <!-- Text input-->
          <div class="form-group">
            <div class="col-md-2">
              <input type="radio" placeholder="" name="answer" value= "false" id=""> False

          </form>';

1 个答案:

答案 0 :(得分:0)

也许我明白你想要什么。 您必须只使用1个表单,并使用对象数组进行多次输入(问题)。

question_page.php

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

echo '<form class="form-horizontal" role="form" method="POST" action="response_question_page.php">';

    while($row=mysqli_fetch_assoc($result))

      {

      if ($row['type'] == "true/false") {
         echo '

              <div class="form-group">

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

                </div>
              </div>
    <div id="form-label">
                <p class="alignleft"><b>Mark this question as:</b></p>

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

              <!-- Text input-->
              <div class="form-group">
                <div class="col-md-2">
                  <input type="radio" placeholder="" name="question[][answer]" value = "true" id="" required> True
                </div>

              </div>


              <!-- Text input-->
              <div class="form-group">
                <div class="col-md-2">
                  <input type="radio" placeholder="" name="question[][answer]" value= "false" id=""> False';

echo '<input type="hidden" name="question[][question]" value="'.$row["question"].'">';

    }
}

             echo '<input type="submit" value="Send all response"></form>';

在你的response_question_page.php中传递的对象是:

{"question" => [
  {
    "question" => "what is your name",
     "answer" => "true"
  }
]}