动态生成的单选按钮不起作用


Dynamically generated radio button dont work

我正在通过PHP动态生成单选按钮,但它不起作用。一旦选中,我就无法取消选中单选按钮,一旦我选择了另一个同名的单选按钮,其他按钮就不会自动取消选中。

echo "<form action="."".">";
echo "<table border='1'>
<tr>
<th>Reg Num</th>
<th>Select</th>
<th>Reject</th>
</tr>";

/* <th>Name</th>
<th>State</th>
<th>Constituency</th> */
while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
 // echo "<td>" . $row['voter_id'] . "</td>";
 //       echo "<td>" . $row['name'] . "</td>";
 //  echo "<td>" . $row['state'] . "</td>";
 //   echo "<td>" . $row['constituency'] . "</td>";
 echo  "<td>" . $row['numreg'] . "</td>";
   echo "<td><input type="."radio"." name=".$row['voter_id']. "value="."1"."></td>";
    echo    "<td><input type="."radio"." name=".$row['voter_id'] ."value="."2"."></td>";
    echo "</tr>";

  }
echo "</table>";
echo "</form>";

请尝试以下操作

echo "<form action=''>";
echo "<table border='1'>
<tr>
<th>Reg Num</th>
<th>Select</th>
<th>Reject</th>
</tr>";

/* <th>Name</th>
<th>State</th>
<th>Constituency</th> */
while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
 // echo "<td> $row['voter_id']</td>";
 //       echo "<td>" . $row['name'] . "</td>";
 //  echo "<td>" . $row['state'] . "</td>";
 //   echo "<td>" . $row['constituency'] . "</td>";
 echo  "<td>" . $row['numreg'] . "</td>";
   echo "<td><input type='radio' name='voter_id' value='$row[voter_id]'></td>";
    echo "<td><input type='radio' name='voter_id' value='$row[voter_id]'></td>";
    echo "</tr>";

  }
echo "</table>";
echo "</form>";

为此,单选按钮的名称必须相同。在创建单选按钮的 while 循环中更改代码,如下所示

 echo '<td><input type="radio" name="voter_id" id="'.$row["voter_id"]. '"value="1"></td>';
 echo '<td><input type="radio" name="voter_id" id="'.$row["voter_id"] .'"value="2"></td>';

我们不需要double quotes类型。尝试喜欢

echo "<td><input type='radio' name='".$row['voter_id']. "' value='1' "></td>";

最后我做到了,它起作用

echo '<td><input type="radio" name="'.$row["voter_id"]. '"value="1"></td>';
echo '<td><input type="radio" name="'.$row["voter_id"] .'"value="2"></td>';