tags:

views:

51

answers:

2

In a HTML form, if we give names to input boxes with [], like this

<input name="foo[]" type="text" />
<input name="foo[]" type="text" />

In PHP, we can obtain values of these input boxes in an array, with $_POST['foo'].

How to do the similar in Perl? I use CGI.pm

+6  A: 

Just assign the result of param to an array.

my @values = param('foo[]');         # If you use the functional-style
my @values = $query->param('foo[]'); # If you use the OO-style of CGI.pm

There's no requirement that the name end with [].

cjm
Thx! It works! Perl is even smarter than PHP for this!
powerboy
Be careful. You should remember to first check for the proper REQUEST_METHOD (GET/POST). CGI::param() is used to retrieve either `GET` or `POST` request parameters from the header.
vol7ron
@vol7ron, why would you need to check the request method? Ordinarily, it doesn't matter which method was used.
cjm
Maybe he means in PHP. In PHP it might be in $_POST but it might also be in $_GET, or of course, both.
AmbroseChapel
@AmbroseChapel: no, I meant Perl; it's because of the different PHP handlers that I wanted to make the distinction.
vol7ron
@cjm: because verifying the request method is an added layer of security, albeit a small one. It ensures that someone is not tinkering with, or attempting to hack, your code. Also, the resulting action of your script may depend on the request method used.
vol7ron
@vol7ron, anybody who would be stopped (or even noticeably slowed) by that "security measure" would never manage to hack your code anyway. Unless you need to vary the behavior of the script based on the request method, checking it is pointless. It's just extra complexity in your code.
cjm
@cjm: think what you will. any hack attempt is well worth identifying earlier, rather than later. many threats today come from kids that don't know better that soon come across some software that does it for them. i'm not trying to convince you of anything, i merely pointed out the difference of Perl vs PHP... there are many other reasons other than security to consider.
vol7ron
A: 

The hot way to do this these days is to post JSON to a CGI. See the docs on the CPAN:

http://search.cpan.org/~makamaka/JSON-2.21/lib/JSON.pm

cjac
Hot in the sense of being over complicated, requiring extra processing on the server, and depending on constructing the form response with JavaScript on the client? This is a terrible idea, keep it simple, stick with HTML — there is no benefit at all to mucking about with JSON in this instance. Oh! I get it - "hot" == "do this and you will burn yourself"!
David Dorward