473,379 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Dynamic Radio Buttons

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.

Apr 6 '07 #1
7 3502
On Apr 6, 9:36 am, "Jerim79" <m...@hotmail.comwrote:
>
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--
} ";
You just answered your own question. Well, almost. Syntax is a
little off, but you were on the right track.

while ($Number!=0){
echo "<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--;
}
Apr 6 '07 #2
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
Apr 6 '07 #3
On Apr 6, 9:08 am, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
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
Thanks, that did the trick. I was just putting my variable before []
instead of after. Odd thing is that when I read it into the database,
I just use $Age[$i], where I thought I would have to use $Age[$i]
$Number. Not sure what is going on under the hood to make $Age[]
$Number into just $Age.

Apr 6 '07 #4
Jerim79 wrote:
On Apr 6, 9:08 am, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
>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

Thanks, that did the trick. I was just putting my variable before []
instead of after. Odd thing is that when I read it into the database,
I just use $Age[$i], where I thought I would have to use $Age[$i]
$Number. Not sure what is going on under the hood to make $Age[]
$Number into just $Age.
Hi Jerim,

It is just the PHP way of passing arrays around from a form to a receiving
script.
If PHP receives for example the following 3 name/value pairs (via POST or
GET), they are automagically turned into an array:

1) name: Age[] value: 10
2) name: Age[] value: 20
3) name: Age[] value: 30

If you extract the Age from the posting, like this:
$passedAge = $_POST["Age"];
PHP 'sees' that Age[] is posted and extracts all the passed values into an
array ($passedAge in this case).
So if a name ends with the [] PHP knows you mean an array.

It is just the PHP way. :-)

Consider this wrong example:
1) name: Age value: 10
2) name: Age value: 20
3) name: Age value: 30

If you try to extract it now like this:
$passedAge = $_POST["Age"];

You'll end up with only the last value (30) and not an array.

So what went wrong in your first try was simply that you didn't end with the
[].

Hope that clearifies it a bit.

Good luck & happy coding!

Regards,
Erwin Moller
Apr 7 '07 #5
Erwin Moller wrote:
Jerim79 wrote:
>On Apr 6, 9:08 am, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spa myourself.comwrote:
>>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
Thanks, that did the trick. I was just putting my variable before []
instead of after. Odd thing is that when I read it into the database,
I just use $Age[$i], where I thought I would have to use $Age[$i]
$Number. Not sure what is going on under the hood to make $Age[]
$Number into just $Age.

Hi Jerim,

It is just the PHP way of passing arrays around from a form to a receiving
script.
If PHP receives for example the following 3 name/value pairs (via POST or
GET), they are automagically turned into an array:

1) name: Age[] value: 10
2) name: Age[] value: 20
3) name: Age[] value: 30

If you extract the Age from the posting, like this:
$passedAge = $_POST["Age"];
PHP 'sees' that Age[] is posted and extracts all the passed values into an
array ($passedAge in this case).
So if a name ends with the [] PHP knows you mean an array.

It is just the PHP way. :-)

Consider this wrong example:
1) name: Age value: 10
2) name: Age value: 20
3) name: Age value: 30

If you try to extract it now like this:
$passedAge = $_POST["Age"];

You'll end up with only the last value (30) and not an array.

So what went wrong in your first try was simply that you didn't end with the
[].

Hope that clearifies it a bit.

Good luck & happy coding!

Regards,
Erwin Moller
Just for clarification:
ending a passed variable with a [] tells PHP it is an array.
This is NOT a html construct, html doesn't care.
Right ?

bill
Apr 7 '07 #6
bill wrote:
Erwin Moller wrote:
>Jerim79 wrote:
>>On Apr 6, 9:08 am, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@sp amyourself.comwrote:
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
Thanks, that did the trick. I was just putting my variable before []
instead of after. Odd thing is that when I read it into the database,
I just use $Age[$i], where I thought I would have to use $Age[$i]
$Number. Not sure what is going on under the hood to make $Age[]
$Number into just $Age.

Hi Jerim,

It is just the PHP way of passing arrays around from a form to a
receiving script.
If PHP receives for example the following 3 name/value pairs (via POST or
GET), they are automagically turned into an array:

1) name: Age[] value: 10
2) name: Age[] value: 20
3) name: Age[] value: 30

If you extract the Age from the posting, like this:
$passedAge = $_POST["Age"];
PHP 'sees' that Age[] is posted and extracts all the passed values into
an array ($passedAge in this case).
So if a name ends with the [] PHP knows you mean an array.

It is just the PHP way. :-)

Consider this wrong example:
1) name: Age value: 10
2) name: Age value: 20
3) name: Age value: 30

If you try to extract it now like this:
$passedAge = $_POST["Age"];

You'll end up with only the last value (30) and not an array.

So what went wrong in your first try was simply that you didn't end with
the
[].

Hope that clearifies it a bit.

Good luck & happy coding!

Regards,
Erwin Moller

Just for clarification:
ending a passed variable with a [] tells PHP it is an array.
This is NOT a html construct, html doesn't care.
Right ?
Right.
HTML just sends all the form's name/value pairs to whatever is defined as
'action' in the form.

Regards,
Erwin Moller

>
bill
Apr 7 '07 #7
Erwin Moller wrote:
bill wrote:
>Erwin Moller wrote:
>>Jerim79 wrote:

On Apr 6, 9:08 am, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@s pamyourself.comwrote:
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
Thanks, that did the trick. I was just putting my variable before []
instead of after. Odd thing is that when I read it into the database,
I just use $Age[$i], where I thought I would have to use $Age[$i]
$Number. Not sure what is going on under the hood to make $Age[]
$Number into just $Age.
Hi Jerim,

It is just the PHP way of passing arrays around from a form to a
receiving script.
If PHP receives for example the following 3 name/value pairs (via POST or
GET), they are automagically turned into an array:

1) name: Age[] value: 10
2) name: Age[] value: 20
3) name: Age[] value: 30

If you extract the Age from the posting, like this:
$passedAge = $_POST["Age"];
PHP 'sees' that Age[] is posted and extracts all the passed values into
an array ($passedAge in this case).
So if a name ends with the [] PHP knows you mean an array.

It is just the PHP way. :-)

Consider this wrong example:
1) name: Age value: 10
2) name: Age value: 20
3) name: Age value: 30

If you try to extract it now like this:
$passedAge = $_POST["Age"];

You'll end up with only the last value (30) and not an array.

So what went wrong in your first try was simply that you didn't end with
the
[].

Hope that clearifies it a bit.

Good luck & happy coding!

Regards,
Erwin Moller
Just for clarification:
ending a passed variable with a [] tells PHP it is an array.
This is NOT a html construct, html doesn't care.
Right ?

Right.
HTML just sends all the form's name/value pairs to whatever is defined as
'action' in the form.

Regards,
Erwin Moller

>bill
Thanks, good to know.

bill
Apr 8 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jean Luc Skywalker Baggins | last post by:
Didn't know who to ask, the java experts or the coldfusion experts... so here it is and my sincerest appologies for the crosspost. I am also going to crosspost to alt.startrek for good measure....
4
by: Oscar Monteiro | last post by:
I Have to sets of Radio buttons like so: <input type="radio" name=p1 value=1> <input type="radio" name=p1 value=2> <input type="radio" name=p1 value=3> <br> <input type="radio" name=p2 value=1>...
6
by: Craig Keightley | last post by:
I have a page that has n number of radio groups (yes/No) how can i prevent the form being submitted if more than one radio group is not selected? By default all radio groups are unchecked ...
22
by: Saul | last post by:
I have a set of radio buttons that are created dynamically, after rendered I try loop thru this set by getting the length of the set, but I keep getting an error stating the element is undefined. I...
10
by: Jen | last post by:
I have a form that has two radio buttons. When the first one is clicked, I would like the page to refresh (keeping the form data in tact) and then displaying 2 new fields that need to be filled...
26
by: Jerim79 | last post by:
I need to create a form that takes a number that the user enters, and duplicates a question the number of times the user entered. For instance, if the customer enters 5 on the first page, when...
2
by: dpazza | last post by:
Hi, I'm creating a quiz on using a form in VB 2005 express. I have four sets of questions and answers (labels and radio buttons) and I change between which set of questions is currently shown on...
0
by: LavanyaM | last post by:
Hi, In our app we need to create dynamic radio buttons. There should be three radio buttons and this set is dynamic accroding to the data this is my code: <th width="5%"...
5
by: plsHelpMe | last post by:
How to create dynamic javascript arrays using dojo toolkits Hello frens, I am in a big trouble. My objective is: I am having some categories shown by differnent radio buttons, on the click of...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.