1

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

4
  • 3
    what's not working; what's the problem you're having? and where is $exam_id coming from? Your question is unclear. Commented Jul 19, 2017 at 14:45
  • i dont 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 wont work since the form tag is closed by then. what i wanted to do is now 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 Commented Jul 19, 2017 at 14:49
  • Looks at your error log, you have an unclosed while loop for a start, so I guess this does not actually compile Commented Jul 19, 2017 at 14:51
  • Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". And more importantly, please read the Stack Overflow question checklist. You might also want to learn about Minimal, Complete, and Verifiable Examples. Commented Jul 19, 2017 at 15:06

1 Answer 1

1

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
 }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I assume if condition should be if($count == Count($row)){

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.