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

Hi monks,

I've an web form with two select menus - copy1 and copy2. If users want to add an item from copy1 to copy2, they simply select the item from copy1 and click on a "ADD" link to add the selected item to copy2.

<select name="copy1" mutiple size="3"> <option value="1" selected>Item1</option> <option value="2">Item12</option> <option value="3">Item3</option> <option value="4">Item4</option> <option value="5">Item5</option> </select> <select name="copy2" multiple size="3"></select>
( The implementation comes with javascript code which I'm leaving out for brevity. )

Say 3 items are selected and they appear in copy2. The user then hits Submit.

How do I capture the 3 items from copy2 in the Perl script?

I hope my question is clear :)

Thanks in anticipation.

Update: There's an example at the url below:

http://www.mattkruse.com/javascript/selectbox/index.html

The one I'm trying to implement is "Copying values from one select box to another".

Replies are listed 'Best First'.
Re: Perl cgi - getting the values from select
by Zaxo (Archbishop) on Aug 07, 2004 at 04:41 UTC

    Only clientside script can alter copy2 to mirror copy1. To be honest, I don't see why that's needed. I assume that the javascript you omitted shows what data transformations are done in the copy. There is probably no reason they can't be done on the server, instead.

    After Compline,
    Zaxo

      Thanks, Zaxo!

      I'm keen to know more about doing it on the server end. Could you elaborate on it's done?

      cheers

Re: Perl cgi - getting the values from select
by Joost (Canon) on Aug 07, 2004 at 12:23 UTC
    The values in a select box are only sent to the server if they are selected, so in general you can't determine the values in a selectbox from a CGI.

    You could make the javascript select all values in copy2 on submit (use the onsubmit attribute of the form tag), or you could put the values in a seperate hidden field while you're adding them to the select box. The first option seems easiest.

    Joost

      I concur absolutely with Joost. On submit, grab the values in copy2 and put them in a hidden field would be my take, but both of Joost's suggestion are workable—it's personal style.

      —Brad
      "Don't ever take a fence down until you know the reason it was put up. " G. K. Chesterton
      Thanks, Joost!

      I was wondering of what use is copying the values from one select box to another if you can't get them from the script (because they aren't "selected"). It turns out that you could do that via a hidden field...

      Cheers

Re: Perl cgi - getting the values from select
by Prior Nacre V (Hermit) on Aug 07, 2004 at 04:39 UTC

    You'll need to do something like:

    my $q = CGI->new; my @selections = $q->param('copy1');

    Regards,

    PN5

      Thanks, Prior Nacre V :)

      I've that, it didn't work :) I'm still working on it...

        Here's a few things you can check:

        • The method attribute of the form element is set to 'post'
        • The action attribute of the form element is set to the correct URI
        • If you use the enctype attribute of the form element, it should be set to 'application/x-www-form-urlencoded'
        • Use the $q->param method without arguments to get a list of all parameter names returned to your CGI script

        Regards,

        PN5