Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Input arrays to CGI

by Nightblade (Beadle)
on Jul 24, 2002 at 02:28 UTC ( #184667=perlquestion: print w/replies, xml ) Need Help??

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

I have list of input fields <input name=item[$id] value="">
<input name=item[3] value=""> <input name=item[45] value=""> <input name=item[234] value="">
In PHP i can access this like array: $item[3],$item[45],...

can i get params into array? @items = param("item"); doesn't work.

I can use cycle to get all members of array,
but often @items contains only few items.

Maybe Perl have better way to get array in CGI?

Replies are listed 'Best First'.
Re: Input arrays to CGI
by Jazz (Curate) on Jul 24, 2002 at 03:08 UTC

    If you're using (and you should be when doing most CGI work), you can retrieve the list of all CGI parameters by using:

    my @fields = param;

    Or you can access a value individually by using:

    my $single_item = param( 'item[123]' );

    Or print out all params and their values by using:

    print join br, map { "$_ = " . param( $_ ) } param();

    If you're not using, you can do so easily by adding use CGI qw/ :standard /; at the top of your program. Documentation for is here.

Re: Input arrays to CGI
by BrowserUk (Pope) on Jul 24, 2002 at 04:26 UTC

    I'm going to assume that you are already using CGI, as you mention @items = param("item");. I don't know anything about PHP, so this may be a reference to a PHP function of the same (fairly obvious)name.

    Anyway, going ahead with my assumption, if you are using CGI already, and the above quoted line doesn't work, then there is something wrong as (from perlman:lib:CGI)

    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 na +med parameter. If the parameter is multivalued (e.g. from multiple se +lections in a scrolling list), you can ask to receive an array. Other +wise the method will return a single value. If a value is not given in the query string, as in the queries ``name1 +=&name2='' or ``name1&name2'', it will be returned as an empty string +. This feature is new in 2.63.

    If this is the case, you should post a larger section of your code and possibly the html it is processing (don't forget your <CODE></CODE> tags and possibly <READMORE></READMORE> tags) and the girls and guys here will have a better chance at being able to help you.

Re: Input arrays to CGI
by sedhed (Scribe) on Jul 24, 2002 at 07:03 UTC

    Looks like you're defining multiple fields with similar names, but does not know that they go together as a group. So, to group certain widgets together for processing, I have used logic like:

    <!-- html --> <input name=mylist_1 ... > <input name=mylist_2 ... > <input name=mylist_3 ... >

    And then in perl:

    @listitems = map(/mylist_(.*)/,$q->param(); # later... $value = $q->param( 'mylist_'.$listitems[3] );

    There's probably much prettier ways of doing it. I'd love to hear what others do...

Re: Input arrays to CGI
by BUU (Prior) on Jul 24, 2002 at 05:08 UTC
    if you just name them all the same thing "name='item'" then when you do $q->param('item'); you will get a reference to a list containing all the values (no clue what order tho)
      I haven't used that functionality. Nice. Upon inspection though, I see that when called in scalar context, param('list') returns the first item only. In list context it works, returning an array of the values of all fields named 'list'.
      http://localhost/somefile.cgi?list=1&list=2&list=666 $list = param('list'); # $list is 1 @list = param('list'); # @list is (1, 2, 666)

      With a list of checkboxes though, say for delete selection in a data table, I'd still have to use the map {/list_(.*)/}, param() trick, because I'd have to know which fields are checked.

      Thanks for pointing that one out BUU

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-12-03 05:05 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (28 votes). Check out past polls.