Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do you retreive information from radio buttons in perl. The info is coming from a HTML page. I know you use the $q->param(' '); command, but this only gets the first button and appends the rest into a string.

Title edit by tye

  • Comment on How to retrieve information from radio buttons

Replies are listed 'Best First'.
Re: Param Help
by DigitalKitty (Parson) on Apr 10, 2002 at 03:45 UTC
    Hi.

    Radio buttons are designed so you can only select one of them at a time ( unlike checkboxes ). If you write some code like this:

    #!/usr/bin/perl -wT print "Content-type: text/html\n\n"; use strict; use CGI; use CGI::Carp qw( fatalsToBrowser ); my $name; my $favorite_food; my $q = new CGI; $name = $q->param("name"); $favorite_food = $q->param("favorite_food"); print <<EOF; <HTML> <HEAD> <TITLE> </TITLE> </HEAD> <BODY> <H4>Thank you $name. I also like $favorite_food.</H4> </BODY> </HTML> EOF


    If name and favorite_food were designated as fields originating from radio buttons, their 'name/value' would be stored in the respective variables. Hope this helps, -DK
Re: Param Help
by rinceWind (Monsignor) on Apr 10, 2002 at 09:49 UTC
    As DigitalKitty says, you can only select one radio button at a time. Therefore, by implication, you are always dealing with groups of buttons. Here is a CGI.pm example, which generates the calling form from perl:
    #!/usr/local/bin/perl use strict; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); print header, start_html, h1("Radio Button Test"), "\n"; print start_form(-method=>'GET'),radio_group( -name => 'test', -values => ['A','B','C'], -linebreak => 'true'); print submit( -name => 'action', -value => 'go'), end_form, end_html, "\n";
    I have used METHOD=GET, so that people running this can see the query string - hence what is being passed in.

    Each button has a distinct value, but share a common name.