Gaurav Maharjan
Gaurav Maharjan

Reputation: 31

PHP get the values from input forms within a while loop

Hello i have a PHP code where i echo form inputs in a while loop. the logic of my code is that i select the question from the table then while it fetches the rows, every time it will echo out a form for a quiz of multiple choice question. so now if there are 20 questions in the table my code will echo 20 forms with questions . now when i press the submit button, i want to get the values of each selected radio button from each form and check with the answer column of my table. here is my code:

<?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>';
    }  
?>

My problem

I don't know where to put the form submit button to get $_POST values. If I put inside the loop it will print for every question, or if I put it outside the loop it won't work since the form tag is closed by then.

what I wanted to do is for every question i want to check the answer by taking if $_POST['answer'] == $row['answer'] , the $_POST['answer'] is the form value of the radio buttons

Upvotes: 1

Views: 762

Answers (1)

Joshua Duxbury
Joshua Duxbury

Reputation: 5260

Here is one solution by using a counter and only add the submit tag at the end when all the rows have been printed

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

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

Upvotes: 1

Related Questions