Tag: radio buttons

Database driven radio buttons

Posted by – July 10, 2007

So I had a great post explaining how to get the value of a radio button if they are dynamically created (such as through a database query) and unforunatly it was lost. Just today I noticed that it was somehow changed to another post about breadcrumbs. I’d like to think I will come back and create another quality post on how to get values of dynamically created, database driven radio buttons.. but until then, here is the short version:

Basically what you are going to want to do it assign a loop counter variable, and add it to the end of each radio button name:

$loopCount = 0;
while($result = mysql_fetch_array($query))
   echo "$result[name]";

Now when we are error checking values, we can start at 0 and loop until the variable no longer exists.

while($_POST['radioSet' . $i])
  //Error check value, post to database, whatever needs to be done

Here you can see, regardless of how many “radioSet” inputs we have, our error checker will get them all. In this example I only gave one radioButton for each input name. Of course for each loop, you can have multiple numbers of inputs. Also, you can give each element an ID using a similar naming tactic, which would allow you to control them all with javascript using document.getElementById.