View Full Version : Can radio buttons be added to a form dynamically?

01-20-2010, 04:59 AM
I am working a webpage which uses MySQL and PHP. On one page vendors could add there business to a list of available firms. On another page a customer could make changes to their account and select a vendor to handle some of their business.

I could do this with a drop down list. Each interested vendor could go through the steps to have his name stored in the database. But I there will not be a long list of posible vendors, so I would rather use radio buttons.

The customer can complete the other portions of the form and then select using a radio button to indicate which vendor he would like to use.

Can radio buttons be added to a from dynamically, along with a label, so as the list of vendors increases, the number of displayed radio buttons would increase also?

01-20-2010, 05:02 AM
Anything like this must be done using JavaScript in the browser, you can't do it with PHP (or it would be rather difficult to implement and require multiple page requests).

Len Whistler
01-20-2010, 06:15 AM
This form will add radio buttons as the database increases, and only output what has been selected.


if($_POST['venders']) {

echo "You have chosen:<br>";

$post_result = $_POST['venders'];

foreach ($post_result as $company) {
echo "<br>$company";

<form method="post" action="index.php">


//db and query
$venders_list = array('Burger King','Fatburger','A&W Burgers','Cheese Burger','In and Out Burgers');

foreach ($venders_list as $value) {
echo "<input type=\"checkbox\" name=\"venders[]\" value=\"$value\">$value</input><br>\n";
<br><input type="submit" name="submit" value="Submit">

01-20-2010, 07:19 AM

Thank you. I was about to turn the lights outs and go to bed, but check the forum instead. I cut and pasted your code into a blank form and it seems to do what I need. I will work on accessing the database tomorrow, but it sure looks like what I was after. Thanks again.

01-22-2010, 06:43 AM

If I can call on you with a follow-up question, it would be greatly appreciated.

Using the code you provided, I was able to modify the fix set of values in your burger joint array with a variable containing an array from my database. It looks and work great. I removed the sort and I modified the check boxes to radio buttons of the same group, and created the first entry in the database and labeled it "NONE". That way the first in the list is always a choice of "none".

My question - is there a way to force a checked = "checked" for this value of "none", thus making it the default selection? Right now, none of the buttons are checked, so unless the user checks one, it is the same as having "none" checked, but it would remove any uncertainty about what happens if nothing is done.

Thanks again for your help.


Len Whistler
01-22-2010, 06:56 AM
I would put the first check box outside the loop, and delete that database entry. The syntax might be incorrect below, but it should give you an idea what to do.

<form method="post" action="index.php">
<input type="checkbox" name="venders" value="none" checked>


// db query loop

EDIT: This applies to the radio buttons also.


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum