Hello All,

I used a for loop to create a set of radio buttons.

For example:

for($i = 1; $i < 4; $i++)
{
print '<INPUT TYPE="radio" NAME="first" VALUE=$i >', " $i|";
}

My question is how do I display which radio button was chosen if all of them have the same value of $i.

Any help would be appreciated.

Thanks

Use the CGI module and it will automatically get multi-valued form fields into your CGI script for you if you call the multi-valued form field in list context, for example:

use CGI qw/:standard/;
print header;
my @first = param('first');#<-- your radio buttons: name='first'
print "$_<br>\n" for @first;

See the CGI module documentation for all the gory details. It s a big module and has lots of functions, takes a while to learn to use well but is a must for CGI coding with perl.

Thanks KevinADC, I tried that initially and it did not print out the selected radio button's information.

Well, I thought so initially so I removed the array of radio buttons and listed them individually 1 - 10 and everything worked fine. When I went back to the array it simply would not display which radio button was chosen.

I see I may have misunderstood your question. If all the radio buttons have the same name and the same value, whats the point anyway? There would be no way to know which one was checked because they all have the same name and the same value. That is just the wrong premise to begin with. That is what your original question asked even though the code does not create the same value $i for each radio button. But maybe you just made a mistake in how you asked the question.

But if the radio buttons had the same name and different values you can get the values but they are all associated with the one name. That is called a multi-value form field: one name but many values.

Or if they have a different name and the same values you can get the radio buttons that were checked because they are no longer a group. Those are just single name single value form fields.

This article has been dead for over six months. Start a new discussion instead.