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

Hi monks,

I'd like to know if it's possible to retrieve params from a form
like this:
$get = $cgi->param('$item');
where $item is set to value1 then value2 then value3 etc.
It doesn't seem to retrieve whether or not the box is checked :( If I use this:
$get = $cgi->param('value1');
there is no problem.
value1, value2 etc. are names of checkboxes in the first cgi page.
Pressing submit calls a second cgi where I want to retrieve on/off values for the checkboxes.
Maybe I can't use $ variables in the param call. Anyone ?

thanx,
basm101

Replies are listed 'Best First'.
Re: $variables when retrieving cgi params
by davorg (Chancellor) on Apr 07, 2003 at 15:35 UTC

    Variables aren't expanded in single-quoted strings. You want:

    $get = $cgi->param($item);

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: $variables when retrieving cgi params
by derby (Abbot) on Apr 07, 2003 at 15:37 UTC
    try

    $get = $cgi->param($item);

    notice no single quotes. The single quotes prevent variable interpolation and hence you were trying to retrieve a param actually named $item.

    -derby

Re: $variables when retrieving cgi params
by Ovid (Cardinal) on Apr 07, 2003 at 16:40 UTC

    A useful snippet to gather all form values from variable names:

    my %form_data = map { $_ => form_data($cgi,$_) } @form_names; sub form_data { my ($cgi,$name) = @_; my @data = $cgi->param($_); return $#data ? \@data : $data[0]; }

    If there is only one value for a name, it is returned as a scalar. If there are multiple values, it's returned as an array reference.

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

Re: $variables when retrieving cgi params
by Juerd (Abbot) on Apr 07, 2003 at 15:41 UTC

    value1, value2 etc. are names of checkboxes in the first cgi page.

    <input type=checkbox name=value value=foo> foo<br> <input type=checkbox name=value value=bar> bar<br> <input type=checkbox name=value value=baz> baz ...
    my @values = $cgi->param('value');

    Please note that 'value' is not very descriptive. Consider changing it to something useful.

    Juerd
    - http://juerd.nl/
    - spamcollector_perlmonks@juerd.nl (do not use).