0

I echo the value of the radio-buttons from my database and in this form ( a while loop ) is the fastest way to get them. When I do this it sees all the radio-buttons as in the same "group". How do to make it so that it has multiple "groups" so it is possible to answer multiple questions?

I tried adding a div to it but that didn't work.

<form action="Antwoord.php" method="POST">
<?php
   $sql = "SELECT * FROM  questionlist_choice";
   $result = mysqli_query($conn, $sql);
  if ($result->num_rows > 0) {
  while ($row = mysqli_fetch_assoc($result)) {
$vraag = $row['Vraag'];
$vraagA = $row['Vraag_keuzeA'];
$vraagB = $row['Vraag_keuzeB'];
$vraagC = $row['Vraag_keuzeC'];
$vraagD = $row['Vraag_keuzeD'];
$vraagE = $row['Vraag_keuzeE'];
$vraagF = $row['Vraag_keuzeF'];
echo "<div>";
    echo "<br><p>$vraag</p>";
    echo "<input type='radio' name='gender' value='$vraagA'> $vraagA<br>";
    echo "<input type='radio' name='gender' value='$vraagB'> $vraagB<br>";
    echo "<input type='radio' name='gender' value='$vraagC'> $vraagC<br>";
    echo "<input type='radio' name='gender' value='$vraagD'> $vraagD<br>";
    echo "<input type='radio' name='gender' value='$vraagE'> $vraagE<br>";
    echo "<input type='radio' name='gender' value='$vraagF'> $vraagF<br>";
echo "</div>";



  }
}
?>
<input type="submit">
</form>
Niels040
  • 79
  • 10
  • I never mentioned checkboxes... – Niels040 Sep 13 '19 at 08:10
  • 2
    Possible duplicate of [Multiple radio button groups in one form](https://stackoverflow.com/questions/28543752/multiple-radio-button-groups-in-one-form) – Dirk Scholten Sep 13 '19 at 08:10
  • 1
    if you need to group the radios give each group the same name: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio#Defining_a_radio_group – Pete Sep 13 '19 at 08:13
  • but this is from database and from a while loop so that would not work, or i would have to not do the whole loop. You can't name one thing in a loop. – Niels040 Sep 13 '19 at 08:14
  • Well then you need to think about what you are doing because that's how radio buttons work - eg put a counter in your while loop and append it to the name – Pete Sep 13 '19 at 08:16
  • usually you just use a grouping name to distinguish groups, but can you also add what kind of values you have inside your table in this post, maybe we would suggest a better answer – Kevin Sep 13 '19 at 08:16
  • @Niels040 You have a static name in each radio button right now ('gender'). Just make sure that that name is variable per question and you're good. – Dirk Scholten Sep 13 '19 at 08:16
  • i made each name different and when i checked it in a different question it went out in the first one. – Niels040 Sep 13 '19 at 08:20
  • I also did no name and then it works but I cant uncheck them and I can check them all. – Niels040 Sep 13 '19 at 08:21

1 Answers1

0

Sorry I don't understand your question that much, but if you want to GROUP them you need to add index on name tag like this:

$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
   $vraag = $row['Vraag'];
   $vraagA = $row['Vraag_keuzeA'];
   $vraagB = $row['Vraag_keuzeB'];
   $vraagC = $row['Vraag_keuzeC'];
   $vraagD = $row['Vraag_keuzeD'];
   $vraagE = $row['Vraag_keuzeE'];
   $vraagF = $row['Vraag_keuzeF'];

   echo "<div>";
   echo "<br><p>$vraag</p>";
   echo "<input type='radio' name='gender[$i]' value='$vraagA'> $vraagA<br>";
   echo "<input type='radio' name='gender[$i]' value='$vraagB'> $vraagB<br>";
   echo "<input type='radio' name='gender[$i]' value='$vraagC'> $vraagC<br>";
   echo "<input type='radio' name='gender[$i]' value='$vraagD'> $vraagD<br>";
   echo "<input type='radio' name='gender[$i]' value='$vraagE'> $vraagE<br>";
   echo "<input type='radio' name='gender[$i]' value='$vraagF'> $vraagF<br>";
   echo "</div>";
$i++;
}

I use $i as the index, I think it would be better if you use for loop cause it already has $key value, I think using this in while is a bad habit.

GGw
  • 413
  • 5
  • 18