9

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

1 Answer 1

16

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 [].

Sign up to request clarification or add additional context in comments.

8 Comments

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, why would you need to check the request method? Ordinarily, it doesn't matter which method was used.
Maybe he means in PHP. In PHP it might be in $_POST but it might also be in $_GET, or of course, both.
@AmbroseChapel: no, I meant Perl; it's because of the different PHP handlers that I wanted to make the distinction.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.