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

Greetings Monks. I'm using the following to create a scrolling list box.

Here's the important part. The values displayed in the list box are being changed dynamically via JavaScript.

my @rightParams = (-class=>'writeField', -name=>$attr, -id=>$rightbox, -values=>$attrHash->{VALUE}, -size=>$size, -multiple=>'true', -labels=>$labels ); $result .= $cgi->scrolling_list(@rightParams)); $cgi->param($attr, $attrHash->{VALUE}); return $result; }

I then pull the value on submit with this:

my @values = $cgi->param($attr);

All this works, except what's ending up in @values are the those items in the list box that are selected.

I know this is as designed. My question is: Is there a way I can get all the values listed in the list box, whether or not they are selected?

Thanks.

Comment on Getting all values from a CGI list box
Select or Download Code
Re: Getting all values from a CGI list box
by jettero (Monsignor) on Jan 02, 2007 at 18:18 UTC

    I'm under the impression this is not a CGI.pm limitation, but something the browser simply doesn't send in the POST request. You'll probably have to store the unchecked values somewhere so you can lookup against the complete list to see what wasn't sent.

    -Paul

Re: Getting all values from a CGI list box
by ferreira (Chaplain) on Jan 02, 2007 at 18:28 UTC
    Is there a way I can get all the values listed in the list box, whether or not they are selected?

    Probably not. When a HTML form is submitted, it usually does not carry this additional information, but only the selected values in the various form widgets/components.

    Generally, to get all those list values, you will have to read the HTML of the form and parse it to find the list box if the page is static. Because if the page is dynamically filled via JavaScript, you will have to mimick the browser's behavior to get the list.

    But if you're designing the page, like jettero said, you may arrange things to get the value list sent with the submitted data form and then do what you want with this information. That can be done for example with a little bit of Javascript and some hidden form component.

      "That can be done for example with a little bit of Javascript and some hidden form component."

      Yeah, this is what I was trying to do, but can't seem to stuff a J/S filled array into a param.

      Thanks for the info, though.

        You'll need to serialize them (join them together into one string) in JS, then deserialize them (seperate them back into a list) in Perl.
Re: Getting all values from a CGI list box
by ikegami (Pope) on Jan 02, 2007 at 18:34 UTC

    One solution is to serialize the list into a hidden field whenever you change the list.

Re: Getting all values from a CGI list box
by derby (Abbot) on Jan 02, 2007 at 18:36 UTC

    That's the spec (see 17.13.3 Processing form data and 17.13.2 Successful controls).

    -derby
Re: Getting all values from a CGI list box
by rashley (Scribe) on Jan 03, 2007 at 13:46 UTC
    Well, I got it working. I ended up making my own serialize function in J/S.

    Just in case anyone needs it, here's what I did.

    On the server:

    my @selected = map { uri_unescape($_) }split(/,/, $cgi->param($attr));

    On the client:

    document.getElementById("Finish Codes").value = serialize(document.get +ElementById("Finish Codes rightbox").options); document.getElementById("Process Codes").value = serialize(document.ge +tElementById("Process Codes rightbox").options); . . . function serialize(list) { var encodedlist = new Array(list.length); for (var i = 0; i != list.length; i++) { encodeURIComponent(list[i].value); encodedlist.push(list[i].value); } return encodedlist.join(","); }