'insert new name for radio buttons in php loop
Within this loop I have:
while($row = mysqli_fetch_assoc($result)) {
$name = 1;
$a = $row["a"];
$b = $row["b"];
$c = $row["c"];
echo "<br>" . "<h3>Question " . $row["id"] . ": " . $row["question"] . "</h3>";
echo "<br>A " . $a . "<input type='radio' name='q'/><br>";
echo "B " . $b . "<input type='radio' name='q'/><br>";
echo "C " . $c . "<input type='radio' name='q'/><br>";
As radio button functionality works I will need to have a new name= every iteration of the table to ensure all buttons are not unselected.
I am thinking of just using $name and incrementing it each time but I am unsure how I could set the php variable as the name attribute in the input tag.
Any tips?
Solution 1:[1]
Your idea is fine, you may use it. It would be something like this. Your variable would be $_POST["q1"], $_POST["q2"] ...
<?php
$name = 0;
while($row = mysqli_fetch_assoc($result)) {
$name++;
$a = $row["a"];
$b = $row["b"];
$c = $row["c"];
echo "<br>" . "<h3>Question " . $row["id"] . ": " . $row["question"] . "</h3>";
echo "<br>A " . $a . "<input type='radio' name='q".$name."'/><br>";
echo "B " . $b . "<input type='radio' name='q".$name."'/><br>";
echo "C " . $c . "<input type='radio' name='q".$name."'/><br>";
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ahmet Feyzi |
