views:

370

answers:

1

What is the best, in Perl, way to get the selected values of a multiple select form field?

<select name="mult" multiple="multiple">
   <option value="1">Opt. 1</option>
   <option value="2">Opt. 2</option> <!-- selected -->
   <option value="3">Opt. 3</option> 
   <option value="4">Opt. 4</option> <!-- selected -->
   <option value="5">Opt. 5</option>
</select>

I get regular form fields like this: $param1 = param('param1');

+5  A: 

If you are using the CGI Module (and I really hope you are) then you can access the multiple values by assigning the param hash to an array and CGI does the rest. So in your example:

my @mult = $q->param('mult');

will store the selected values (2, 4) in the @mult array.

Gurunandan
"must" use CGI?
innaM
I meant "...and I hope you are..." I agree "must" sounds needlessly aggressive. Have edited and changed it. :)
Gurunandan
Well, he's using something, 'param()' didn't just materialise out of thin air...
ijw