473,785 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($qu ery) 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$Numb er[]\" value=\"20-30"\>20-30
<input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age$Numb er[]\" 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 3528
On Apr 6, 9:36 am, "Jerim79" <m...@hotmail.c omwrote:
>
echo "while ($Number!=0){
<input type=\"radio\" name=\"Age$Numb er[]\" value=\"20-30"\>20-30
<input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age$Numb er[]\" 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{$Num ber}[]\" value=
\"20-30\">20-30
<input type=\"radio\" name=\"Age{$Num ber}[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age{$Num ber}[]\" 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($qu ery) 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$Numb er[]\" value=\"20-30"\>20-30
<input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age$Numb er[]\" 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="numberOfA ges" value="<?php echo $number; ?>">
<?php
for ($count=0;$coun t<$number;$coun t++){
?>
<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["numberOfAg es"];
for ($count=0;$coun t<$numberOfAges ;$count++){
$name = "Age".$coun t;
$theSelectedVal ue = $_POST[$name];
// Do whatever you want with $theSelectedVal ue
}
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_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
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($qu ery) 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$Numb er[]\" value=\"20-30"\>20-30
<input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age$Numb er[]\" 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="numberOfA ges" value="<?php echo $number; ?>">
<?php
for ($count=0;$coun t<$number;$coun t++){
?>
<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["numberOfAg es"];
for ($count=0;$coun t<$numberOfAges ;$count++){
$name = "Age".$coun t;
$theSelectedVal ue = $_POST[$name];
// Do whatever you want with $theSelectedVal ue

}

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_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
>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($qu ery) 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$Numb er[]\" value=\"20-30"\>20-30
<input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age$Numb er[]\" 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="numberOfA ges" value="<?php echo $number; ?>">
<?php
for ($count=0;$coun t<$number;$coun t++){
?>
<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:
$numberOfAge s = $_POST["numberOfAg es"];
for ($count=0;$coun t<$numberOfAges ;$count++){
$name = "Age".$coun t;
$theSelectedVal ue = $_POST[$name];
// Do whatever you want with $theSelectedVal ue

}

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.. .@spamyourself. 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($qu ery) 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$Numb er[]\" value=\"20-30"\>20-30
<input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age$Numb er[]\" 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="numberOfA ges" value="<?php echo $number; ?>">
<?php
for ($count=0;$coun t<$number;$coun t++){
?>
<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:
$numberOfAg es = $_POST["numberOfAg es"];
for ($count=0;$coun t<$numberOfAges ;$count++){
$name = "Age".$coun t;
$theSelectedVal ue = $_POST[$name];
// Do whatever you want with $theSelectedVal ue

}

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. ..@spamyourself .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($qu ery) 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$Numb er[]\" value=\"20-30"\>20-30
<input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
<input type=\"radio\" name=\"Age$Numb er[]\" 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="numberOfA ges" value="<?php echo $number;
?>"<?php
for ($count=0;$coun t<$number;$coun t++){
?>
<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:
$numberOfAge s = $_POST["numberOfAg es"];
for ($count=0;$coun t<$numberOfAges ;$count++){
$name = "Age".$coun t;
$theSelectedVal ue = $_POST[$name];
// Do whatever you want with $theSelectedVal ue

}

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_human s_read_this_I_a m_spammed_too_m ...@spamyoursel f.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($qu ery) or die('Query failed: ' .
>mysql_erro r());
>}
>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$Numb er[]\" value=\"20-30"\>20-30
> <input type=\"radio\" name=\"Age$Numb er[]\" value=\"30-40\">30-40
> <input type=\"radio\" name=\"Age$Numb er[]\" 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="numberOfA ges" value="<?php echo $number;
?>"<?php
for ($count=0;$coun t<$number;$coun t++){
?>
<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:
$numberOfAg es = $_POST["numberOfAg es"];
for ($count=0;$coun t<$numberOfAges ;$count++){
$name = "Age".$coun t;
$theSelectedVal ue = $_POST[$name];
// Do whatever you want with $theSelectedVal ue
>
}
>
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
2190
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. Generally speaking, whenever I do this the response seems to come from the startrek NG first no matter how inappropriate the crosspost... go figure. I'm working on a coldfusion application that asks students whether they have taken a class, and if...
4
3282
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> <input type="radio" name=p2 value=2> <input type="radio" name=p2 value=3> then a text area and a button:
6
3293
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 The problem i am facing is that i do not know how many yes/no radio groups will be generated
22
7978
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 am using getElelementsByName since these are radio buttons, but it seems that the dynamic element is not seen!!! This is my code... please let me know if there is anything that I am doing wrong! - thanks ---- ....
10
1629
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 out. If the second button is clicked, I need the same thing to happen, only it will display two different fields needing to be filled out. Does anyone have code that will do this? Or is this even possible with JavaScript? Thanks!!
26
2815
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 they press next the form generates "How old are you?" 5 times on the page. The customer will answer all 5 questions then press next. Finally, all the local variables get dynamically created and written to a database. I have already taken care of...
2
5893
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 the form by changing the visible state of the radio buttons and labels utilising back and next buttons. E.g. Next button makes current radio buttons and labels invisible and
0
1618
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%" class="twolbcolors"><html:radio property="button" value="confirm"></</html:radio></th> <th width="5%" class="twolbcolors"><html:radio property="button" value="wait"></html:radio></th> <th width="5%"...
5
3681
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 a particular radio button i making an ajax call using dojo and retirieving the data corresponding to that category. Now i am suppose to create array of that data using the javascript dynamically. Can anyone please help me for the same. For your...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.