I am making a multiple choice question and on the first page radiobuttons look like this
$resultx = $connx->query($sqlx);
$qcounter = 1;
echo '<form action="result.php" method="post">';
echo "<table>";
while ($row = mysqli_fetch_array($resultx)) {
echo '<tr><td>Question '.$qcounter.'<br/></td></tr>';
//output a row here
echo "<tr><td>".($row['Question'])."</td></tr>";
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="A" id="question-1-answers-A"/><label for="question-1-answers-C3">A) '.($row['AnswerA']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="B" id="question-1-answers-B"/><label for="question-1-answers-C3">B) '.($row['AnswerB']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="C" id="question-1-answers-C"/><label for="question-1-answers-C3">C) '.($row['AnswerC']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="D" id="question-1-answers-D"/><label for="question-1-answers-C3">D) '.($row['AnswerD']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="E" id="question-1-answers-E"/><label for="question-1-answers-C3">E) '.($row['AnswerE']).'</label></td></tr>';
echo '<tr><td><input type="hidden" name="question-id[]" value='.($row['id']).'</td></tr>';
echo '<tr><td><input type="hidden" name="question_bar_code[]" id="question_bar_code" value='.($row['CorrectAnswer']).'</td></tr>';
echo '<tr><td><br/></td></tr>';
$qcounter++;
}
echo "</table>";
echo '<div>';
echo '<input class="hvr-fade1" type="submit" value="Finish">';
echo '</div>';
echo '</form>';
I have a counter (qcounter) to name the radiobuttons, and the name of the radiobuttons are q1, q2, q3, q4 for each question as they are looped through depending on the number of questions.
Now my second page where I analyze the results I have this code to retrieve and see the selected radiobuttons:
<?php
$qcounter = 1;
$answer1 = $_POST["q".$qcounter];
foreach($answer1 as $item1) {
echo "<table>";
echo "<tr><td>".$item1."</td></tr>";
$qcounter2++;
echo "</table>";
}
?>
But it only echos the selected radiobutton for q1
Any way I can have them all echoed?
You can set the counter outside the loop
and use it inside the loop:
$qcounter = 1;
while ($row = mysqli_fetch_array($resultx)) {
echo '<tr><td>Question '.$qcounter.'<br/></td></tr>';
echo "<tr><td>".($row['Question'])."</td></tr>";
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="A" id="question-1-answers-A"/><label for="question-1-answers-C3">A) '.($row['AnswerA']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="B" id="question-1-answers-B"/><label for="question-1-answers-C3">B) '.($row['AnswerB']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="C" id="question-1-answers-C"/><label for="question-1-answers-C3">C) '.($row['AnswerC']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="D" id="question-1-answers-D"/><label for="question-1-answers-C3">D) '.($row['AnswerD']).'</label></td></tr>';
$qcounter++;
}
After the change to the question, here is a better answer:
You should change all the input
you have to multi-dimensional array:
$resultx = $connx->query($sqlx);
$qcounter = 1;
echo '<form action="result.php" method="post">';
echo "<table>";
while ($row = mysqli_fetch_array($resultx)) {
echo '<tr><td>Question '.$qcounter.'<br/></td></tr>';
//output a row here
echo "<tr><td>".($row['Question'])."</td></tr>";
echo '<tr><td><input type="radio" name="q[' . $qcounter . '][]" value="A" id="question-1-answers-A"/><label for="question-1-answers-C3">A) '.($row['AnswerA']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q[' . $qcounter . '][]" value="B" id="question-1-answers-B"/><label for="question-1-answers-C3">B) '.($row['AnswerB']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q[' . $qcounter . '][]" value="C" id="question-1-answers-C"/><label for="question-1-answers-C3">C) '.($row['AnswerC']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q[' . $qcounter . '][]" value="D" id="question-1-answers-D"/><label for="question-1-answers-C3">D) '.($row['AnswerD']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q[' . $qcounter . '][]" value="E" id="question-1-answers-E"/><label for="question-1-answers-C3">E) '.($row['AnswerE']).'</label></td></tr>';
echo '<tr><td><input type="hidden" name="question-id[]" value='.($row['id']).'</td></tr>';
echo '<tr><td><input type="hidden" name="question_bar_code[]" id="question_bar_code" value='.($row['CorrectAnswer']).'</td></tr>';
echo '<tr><td><br/></td></tr>';
$qcounter++;
}
echo "</table>";
echo '<div>';
echo '<input class="hvr-fade1" type="submit" value="Finish">';
echo '</div>';
echo '</form>';
And now - after the user post
ed the form - you should walk-through the $_POST['q']
array to get the relevant values:
foreach ($_POST['q'] as $qid => $answer1) {
foreach($answer1 as $item1) {
echo "<table>";
echo "<tr><td>".$item1."</td></tr>";
$qcounter2++;
echo "</table>";
}
}
You better use
var_dump($_POST['q']);
or evenvar_dump($_POST['q'][1]);
to get a better overview of the data you receive from the browser after the submission of the form.