Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Passing arrays to a CGI

by arturo (Vicar)
on Sep 20, 2000 at 23:14 UTC ( [id://33378]=perlquestion: print w/replies, xml ) Need Help??

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

OK, I'm stumped, and I can't figure out how to do this *easily* from the CGI.pm documentation.

The problem: I want to get a variable-length list of data from an HTML form where I don't know in advance what the data's going to be. That is, while I know each element is going to be a string, I can't predict in advance what those strings will be and how many of them there will be. in PHP (make the sign of the cross if you have to), The HTML would look like this:

<input type='checkbox' name='location[]' value='$string'> ...
(imagine several of these) and when the form is submitted, the variable $location would be an array whose elements are the various strings that were submitted.

Can I do something similarly easy in Perl, or am I going to have to resort to some kind of trickery in the script that generates the HTML form?

Philosophy can be made out of anything -- or less

Replies are listed 'Best First'.
Re: Passing arrays to a CGI
by Fastolfe (Vicar) on Sep 20, 2000 at 23:19 UTC
    CGI.pm already does this in a fashion:
    <input name='form_item' value='one'> <input name='form_item' value='two'> <input name='form_item' value='three'>
    And the resulting Perl:
    my $q = new CGI; print join(",", $q->param('form_item')); # one,two,three
    In cases where more than one form element uses the same name, CGI.pm will return them as an array, if you call the param method in a list context. Is this what you meant?

      Gah! I shoulda known it was that simple. I think the comments in the CGI.pm documentation say this (now that you point it out), but they say it a little cryptically =) Thanks!

      Philosophy can be made out of anything -- or less

(Ovid) Re: Passing arrays to a CGI
by Ovid (Cardinal) on Sep 20, 2000 at 23:26 UTC
    If I understand your question correctly, something like the following may be what you're looking for:
    #!/usr/bin/perl -Tw use strict; use CGI; my $query = new CGI; my @keys = $query->param; my %data; # Do NOT use this code. It is very insecure and for illustration purp +oses only! # Individual elements are untainted and may be dangerous! for (@keys) { $data{$_} = $query->param($_); }
    The %data hash will the contain all of the name/value pairs from the forms. With the complete hash, you can manipulate the data at will.

    If, for some reason, you have more than one value for a name, the hash will contain (if I recall correctly) a reference to an array for that name with the elements of the array being the values.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just go the the link and check out our stats.

Re: Passing arrays to a CGI
by elwarren (Priest) on Sep 21, 2000 at 19:15 UTC
    From the cgi.pm docs:
    FETCHING THE VALUE OR VALUES OF A SINGLE NAMED PARAMETER:
    @values = $query->param('foo'); -or- $value = $query->param('foo');
    Pass the param() method a single argument to fetch the value of the named parameter. If the parameter is multivalued (e.g. from multiple selections in a scrolling list), you can ask to receive an array. Otherwise the method will return a single value.

    HTH
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://33378]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2024-04-18 11:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found