Skip to content
Advertisement

PHP radio buttons next to each other in second for loop

I am trying to get radio buttons to display next to each other, but it is creating 2 lines or radio buttons instead of all 5 next to each other. $mcqnum is a value I am getting from another PHP page. That is working fine. Its the second for loop that is not putting them next to each other, it is placing them in 2 line.

enter code here

<?php
SESSION_START();
echo "Exam page";
$mcqnum = $_SESSION['nummcq'];  //Get number of MCQ ?
echo "$mcqnum";

$i = 0;
$j = 0;

for ($i = 1; $i <= $mcqnum; $i++){
    echo"<form method = 'post'> ";
     echo "$i <input type='radio' name='num' value='$i>  <br>";
     for ($j= 1; $j<5;$j++){
         echo"<label style='display:inline-block'>";
         echo "<input type='radio' name='num' value='$i' ><br>";
         echo"</label>";
          }
     echo"</form>";
}
?>

This is the output Output

Advertisement

Answer

You have multiple form constructs in your codes. This will cause carriage returns, and other problems.

Please move the FORM and /FORM outside the loops:

Hence, change

for ($i = 1; $i <= $mcqnum; $i++){
    echo"<form method = 'post'> ";
     echo "$i <input type='radio' name='num' value='$i>  <br>";
     for ($j= 1; $j<5;$j++){
         echo"<label style='display:inline-block'>";
         echo "<input type='radio' name='num' value='$i' ><br>";
         echo"</label>";
          }
     echo"</form>";
}

to

echo "<form method = 'post'> ";
for ($i = 1; $i <= $mcqnum; $i++){

     echo "$i";

     for ($j= 1; $j<=5;$j++){
         echo"<label style='display:inline-block'>";
         echo "<input type='radio' name='num" . $i . "' value='$j' >";
         echo"</label>";
          }

echo "<br>";
}

     echo"</form>";
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement