简体   繁体   中英

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

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>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM