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

Hello, I am passing some parameters from a javascript form to a perl cgi script. I am pretty new w/ CGI, so I am sure I am doing something wrong when getting the params. The relevant lines:
my $x = new CGI; my @var = $x->param;
but this is giving me the names of the form fields, not their values. What am I missing? Thanks, P/S: in case you're interested, the relevant lines of javascript are these:
function submitFormInfo () { form.action="http://foo/bar.pl"; form.submit(); }

Replies are listed 'Best First'.
Re: cgi and javascript
by tlm (Prior) on Jun 14, 2005 at 14:12 UTC

    CGI's param function is strange. For example, without arguments it returns the names of the parameters. But if you give it one of these names as argument, it returns the value of the parameter. I.e. to get the value of parameter 'foo', use

    $x->param('foo')
    If it is a multivalued parameter, then make sure you collect the results in an array.

    the lowliest monk

Re: cgi and javascript
by davidrw (Prior) on Jun 14, 2005 at 14:16 UTC
    The CGI manpage has these entries in the DESCRIPTION section:
    • FETCHING THE NAMES OF ALL THE PARAMETERS PASSED TO YOUR SCRIPT:
      @names = $query->param
    • FETCHING THE VALUE OR VALUES OF A SINGLE NAMED PARAMETER:
      @values = $query->param('foo'); # -OR- $value = $query->param('foo');
    • FETCHING THE PARAMETER LIST AS A HASH:
      %params = $q->Vars;
Re: cgi and javascript
by ikegami (Patriarch) on Jun 14, 2005 at 14:18 UTC

    tlm showed you how to get the (lone) value of a single parameter. To display all the parameters:

    use CGI (); my $cgi = CGI->new; print("Content-Type: text/plain\n\n"); # Loop over each parameter. foreach my $param_name ($cgi->param) { # Loop over each value of the parameter. foreach my $value (@{$cgi->param($param_name)}) { print "$param_name = $value\n"; } }
Re: cgi and javascript
by dorward (Curate) on Jun 14, 2005 at 14:14 UTC

    Rant: You appear to be depending on JavaScript (you almost certainly don't need to as you can do most things server side), and depending on the browser creating a global variable reference to any element with an id or name (which boils down to assuming the user has Internet Explorer without the security settings ramped up).

    As to getting the names of the form fields:

    the param() method will return the parameter names as a list

    ... that's what is supposed to happen. You should probably reread the docs for CGI.pm.

      About your rant: I know. I don't even like JS, but I am interfacing w/ an existing, proprietary webapp written in js, and I need to do some processing on some form data, then dump it on MySQL. Given that JS is client side, this is not possible. Hence perl. Thanks.
Re: cgi and javascript
by Anonymous Monk on Jun 14, 2005 at 14:31 UTC
    Thanks folks. Appreciate it.