Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Passing 'SELECT MULTIPLE' parameters and CGI

by viffer (Beadle)
on Aug 17, 2010 at 14:16 UTC ( #855496=perlquestion: print w/replies, xml ) Need Help??

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

Good folks, I've had a good hunt through this website and it does appear to me what I've done is correct (it obviously isn't)!
I'm trying to pass multiple select items via cgi but I only seem to be passing the first entry. The html gets formatted correctly (I think) as
<form action="post" name="RunForm"> <input id="env" type="hidden" name="env" value="PROD"/> <select id="runs" name="runs" multiple=""> <option>2010-08-17 ----- 039733</option> <option>2010-08-17 ----- 039753</option> </select> <p> <input type="button" onclick="UntarFiles(env,runs)" value="Combine Fil +es"/> <input type="reset" value="Cancel" name="Cancel"/> </p> </form>
The Javascript called when they onclick is
function UntarFiles() { if (document.RunForm.runs.selectedIndex==-1) { alert("Please make a selection from the list."); return false; } var env = document.getElementById("env").value; var runs = document.getElementById("runs").value; var url = "atlanta_combine_postscript.cgi?&env=" + escape(env) + " +&runs=" + escape(runs);"GET", url, true); request.onreadystatechange = IssueAlert; request.send(null); }
and in the called perl cgi program, to receive the parameters I have
use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); my ( $env, @runs ); $env = param('env'); @runs = $query->param('runs');
However whilst what I have in the called perl program may be wrong - since I can see in the error log I'm getting the error "Can't call method "param" on an undefined value"

So the issue I have is that when I select multiple items, I can see that what it is trying to be passed is

atlanta_combine_postscript.cgi?&env=PROD&runs=2010-08-17 ----- 039733
rather than

atlanta_combine_postscript.cgi?&env=PROD&runs=2010-08-17 ----- 0397332010-08-17 ----- 039753

(if indeed that is how I would expect to see multiple items passed in)?
So two questions:
Can anyone see what I've done wrong to only pick up one rather than all selected items
Is what I've done to initially receive the data (as an array) correct?
Any help greatly appreciated as ever. Cheers

Replies are listed 'Best First'.
Re: Passing 'SELECT MULTIPLE' parameters and CGI
by afoken (Canon) on Aug 17, 2010 at 15:12 UTC
    However whilst what I have in the called perl program may be wrong - since I can see in the error log I'm getting the error "Can't call method "param" on an undefined value"

    $query is undefined because you never created it. Add use strict; and use warnings; to the head of your script. Then decide how you want to use the CGI module: Choose between OOP way and non-OOP way. For the OOP way, create an instance of the CGI class: my $query=CGI->new();. For the non-OOP way, call param() as a function, not as a method, i.e. remove $query->.


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Thanks guys, but I'm not even getting the value in 'runs' to worry about changing the query part. I will fix that up (thank you) as soon as I manage to actually pass multiple values to the called program.
      Incidentally I do have warnings and strict, I just pasted snippets.

      The weird thing is I have

      my $selectline = '<SELECT name="runs" MULTIPLE size="15" id="runs">'; print $selectline;

      Running it in debug, that's what appears to be printed, yet when I look at the source on IE8 it has it as

      <select name="runs" id="runs" size="15" multiple="multiple">

      and firefox has it as

      <select id="runs" size="15" multiple="" name="runs">

      In both instances it seems to have changed the order and set MULTIPLE to either "" or "multiple".
      I'm wondering if it's simply not picking up the 'multiple' setting as it correctly passes down individually selected items but not multiple selections,

        In both instances it seems to have changed the order and set MULTIPLE to either "" or "multiple".

        The value of multiple doesn't matter, just the presence or absence of the attribute.

Re: Passing 'SELECT MULTIPLE' parameters and CGI
by ikegami (Patriarch) on Aug 17, 2010 at 16:52 UTC

    If you were submtting the form, it would work. Instead you are doing requests from JS based on information in the form. Your JavaScript only ever constructs requests with a single runs argument, so your script will never see more than one value for runs. Once you fix your JavaScript, the Perl side will work without modification.

    The url should look like this

    ...?env=PROD&runs=2010-08-17 ----- 039733&runs=2010-08-17 ----- 039753

    (Improperly escaped for readability.)

Re: Passing 'SELECT MULTIPLE' parameters and CGI
by CountZero (Bishop) on Aug 17, 2010 at 15:11 UTC
    Could it be that this is the offending line:
    @runs = $query->param('runs');
    Did you perhaps forget to instantiate the $query-object?


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Passing 'SELECT MULTIPLE' parameters and CGI
by choroba (Archbishop) on Aug 17, 2010 at 14:21 UTC
    Not really Perl-related. See the documentation of select multiple attribute: you have to set it to "multiple" to get it working.
      i do have it set to multiple
      <select id="runs" name="runs" multiple="">
      but thanks anyway.
        Oh, you are right, ok.

        Why are you transforming the output of the form with javascript? The form can send its data itself. You may also check the actual value of the runs variable in the javascript code by adding alert(runs) right after the assignment var runs = ....

      Well, while w3schools is full of good stuff, they happen to be wrong about multiple. According to HTML 4.01 multiple is a boolean flag that is set or not, thus

      <select id="runs" name="runs" multiple>
Re: Passing 'SELECT MULTIPLE' parameters and CGI
by choroba (Archbishop) on Aug 17, 2010 at 22:32 UTC
    Looks like it is not trivial to get multiple selected items to javascript, see for example here.
      Yes it is trivial, the jQuery way
      var urlstring = $('#formid').serialize()
      The other way is trivial as well but more verbose

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://855496]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2022-05-18 12:21 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (70 votes). Check out past polls.