Re: Dynamic Radio Buttons



Jerim79 wrote:

My situation is that I have a form that asks the user for a number.
Next, I execute a while loop that displays a group of questions the
amount of times the customer entered. For instance, the loop looks
this:

while ($Number!=0){
<input type="radio" name="Age[]" value="20-30">20-30
<input type="radio name="Age[]" value="30-40">30-40
<input type="radio name"Age[]" value="40-50">40-50
$Number--;
}

Let's say someone entered 3 for $Number. When the loop executes it
will produce:

echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
<input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";

echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
<input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";

echo" <input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
<input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50";

The problem is that with radio buttons, you can only choose one
option. With this code, it only allows the user to choose one option
from among the 9 listed, instead of one from each set. I am trying to
find a way to dynamically name each set. If I use checkboxes, then it
records the information correctly and correctly writes it to the
database. However, checkboxes don't look good for this application in
my opinion, and there is no way for me to keep someone from checking
more than one box, that I know of. Here is the PHP code I am using to
catch the user's input and write it to a database:

$Age = $_POST['Age'];

for ($i=0; $i < $Number; $i++) {
$query="INSERT INTO table VALUES('$Age[$i])";
$result = mysql_query($query) or die('Query failed: ' .
mysql_error());
}

Like I say, that works fine if I use checkboxes, without changing the
HTML name="Age[]". I am looking for a way to define "sets" of radio
buttons with the same name, so that only one from within a set can be
chosen at a time. Is there something such as:

echo "<group name="1">
<input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
<input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
</group>";

echo "<group name=\"2\">
<input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
<input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
</group>";

echo "<group name=\"3\">
<input type=\"radio\" name=\"Age[]\" value=\"20-30\">20-30
<input type=\"radio\" name=\"Age[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age[]\" value=\"40-50\">40-50
</group>";

This may be more of a HTML question, but I am open to any solution
that would help. Any way to use PHP to accomplish my goal? Even if
there is a way to dynamically name each set such as:

echo "while ($Number!=0){
<input type=\"radio\" name=\"Age$Number[]\" value=\"20-30"\>20-30
<input type=\"radio\" name=\"Age$Number[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age$Number[]\" value=\"40-50\">40-50
$Number--
} ";

I would just have to create a loop to run through each array when
writing to the database, but that shouldn't be a problem.

Hi,

You cannot make a group of the radiobuttons since you named them all Age[]
and that IS the group as far as HTML is concerned.
So just code it in such a way they have different names, like this:

<?php
// receive number
$number = (int)$_POST["number"];
?>
<input type="hidden" name="numberOfAges" value="<?php echo $number; ?>">
<?php
for ($count=0;$count<$number;$count++){
?>
<input type="radio" name="Age<?php echo $number; ?>[]"
value="20-30">20-30
<input type="radio name="Age[]<?php echo $number; ?>"
value="30-40">30-40
<input type="radio name"Age[]<?php echo $number; ?>"
value="40-50">40-50
<?php
}
?>


Now them radiogroups have names like Age0[] and Age1[]

And in the receiving script:
$numberOfAges = $_POST["numberOfAges"];
for ($count=0;$count<$numberOfAges;$count++){
$name = "Age".$count;
$theSelectedValue = $_POST[$name];
// Do whatever you want with $theSelectedValue
}


Not tested, but I hope you get my drift. :-)

Regards,
Erwin Moller
.