Search code examples
phpmysqlradio-buttonradiobuttonlist

Form built from database array values is not limiting radio buttons to single selection


I am trying to build a form where the user selects an item based upon an array of values from a database table. Here is the code that I am using to filter out the list:

$arrlength=count($results);

$listSubscriptions = '<form>';
for($x=0; $x<$arrlength; $x++)
  {
    $itemList =    $results[$x]->item_name;
    $listSubscriptions .= '<input type="radio" name="'.$itemList.'" value="'.$itemList.'"> '.$itemList.'<br>';
  }
  $listSubscriptions .= '</form>';

 return $listSubscriptions;

When I print the results of the form I am able to get the radio buttons to display as I would like them to, but the problem is that the selection is not being limited to one radio button selection. Any ideas why?


Solution

  • The radio buttons must all share a single name attribute to belong to the same group:

    $listSubscriptions .= '<input type="radio" name="subscription" value="' . $itemList . '"> ' . $itemList . '<br>';