954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dynamically named radio burtons

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.

newboi
Newbie Poster
7 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

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;
  }else{
    $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.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,796 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: