phphtmlselectradio-buttonname-attribute

HTML/PHP: How to create multiple radio buttons for each row in a table?


I have an HTML table having n rows. Each row has a name and 3 radio buttons. What I want to do is, each row must have a selected radio button. However, when I choose a radio button in the first row and I choose another radio button in the second row, the first radio button becomes unselected. I know that the problem is with the name attribute of the radio button but I don't know how to fix it. What should I do? Thank you in advance.

Here's my code.

echo '<table>';
list($cols,) = $xlsx->dimension();
foreach( $xlsx->rows() as $k => $r) {
    echo '<tr>';
        echo '<td>'.$k.'</td>';
        echo '<td>'.$name.'</td>';
        echo '<td><Input type = "Radio" Name ="vote" value= "pacada"></td>';
        echo '<td><Input type = "Radio" Name ="vote" value= "toledo"></td>';
        echo '<td><Input type = "Radio" Name ="vote" value= "undecided1" checked></td>';
    echo '</tr>';
}
echo '</table>';

Solution

  • Add a variable to the name:

    $row = 0;
    foreach(...) {
        $row++
        ...
        echo '<td><Input type = "Radio" Name ="vote' . $row . '" value= "pacada"></td>';
        ...
    

    That way all rows will have different names.