Reply to Re: Dynamic Radio Buttons

Your name:

Reply:


Posted by Erwin Moller on 04/07/07 09:32

Jerim79 wrote:

> On Apr 6, 9:08 am, Erwin Moller
> <since_humans_read_this_I_am_spammed_too_m...@spamyourself.com> wrote:
>> 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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация