Hello Guys,

I am trying to update multiple table rows using dynamically generated radio buttons.

The radio buttons are in groups of two (yes and no) which also have dynamic names.

In other to give the group unique names, I have added row numbers to the group names as thus:

... some select statement...

while($row = mysql_fetch_array($result)){
echo "<input type=\"radio\" name=\"test".$rowId."\" value=\"yes\" />Yes 
<input type=\"radio\" name=\"test".$rowId."\" value=\"no\" checked=\"checked\" />No";

this works fine in grouping the buttons, but my problem is how to retrieve the value of the buttons when the form is submitted seeing that the group names are part dynamic.

Summarised in one sentence, I am trying to retrieve values of dynamically named radio button groups.

I am losing it. Somebody pls help.

Member Avatar

If test1 is a name:

$test1 = $_POST['test1']; //will give 'yes' or 'no'

If you don't know the 'key' for the 'test' it's going to be difficult. Perhaps placing an actual key would be possible:

... name = "test['$rowId']" ...

as an attribute.

Then in your form handling:

$arr = $_POST['test'];

$sqlkeyvalue = "";

foreach($arr as $key=>$value){
  if($value == 'yes'){
    $sqlval = 1;
    $sqlval = 0;
  $sqlkeyvalue .= ",`field{$key}` = $sqlvalue";

$sql = mysql_query("INSERT INTO table1 SET " . substr($sqlkeyvalue,0,-1) WHERE ...");

I haven't tried this so I can't say if it's even possible, but I think it's pretty close.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.