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

Hello All
I am having a problem with CGI:Vars not reporting correct parameters from a form. These are a set of checkboxes and textfields with names "ipaddress:paramname". There might be one set for one address and another set for another and so on.

The problem is when I process the form and get the params with

%params = $q-Vars();
for each $key (keys %params)
{ print "key is $key with value $params{$key}"; }

With the above I get and output like:
key is ipaddress: with value paramvalu1 paramvalu2..... INSTEAD OF
key is ipaddress:paramname with value paramvalu1
key is ipaddress:paramname with value paramvalu1 ...


A strange thing I noticed is that the problem is with parameters I generate in a function, pass back via an array and then print. Any paramater not done in this way appears correctly in the processing. ALL parameters appear corretly when I do a "view source" on the page.

Any ideas about this?
Thanks
Sawan

edited: Sun Feb 9 14:51:30 2003 by jeffa - title change (was: Problem with CCI:Vars)

Replies are listed 'Best First'.
Re: Problem with CGI::Vars
by data64 (Chaplain) on Feb 08, 2003 at 07:52 UTC

    See the CGI documentation.

    In a hash, you cannot have two entries with the same key. Hence, if your form returns multiple values for the same key, CGI will return a list of values for that key when you call param.

    hope that helps.

    Just a tongue-tied, twisted, earth-bound misfit. -- Pink Floyd

      FWIW with Vars() it returns a \0 (null) separated string for multiple values with the same param name (hash key) which was the cgi-lib.pl way or doing it. This is a compatibility method that should not really be used. As you point out all that has to be done is:

      use CGI; my $q = new CGI; # iterate over all param names for my $param ( $q->param() ) { print "<p>Param $param\n"; # iterate over all values for param name $param for my $value ( $q->param($param) ) { print "<br> value: $value"; } }

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Hello.

        Done that foreach param() stuff, it shows the same as $q->Vars(), ie still not doing the right thing.

        Thanks
        Sawan
      Hello.

      The form does not have multiple keys. The name of the form parameter is composed of two things: an ipaddress and a parameter name. Thus no two fields are alike. Have verified that by "view source".

      What else could it be? Why should the form return a single parameter name when all are unique?

      Thanks, Sawan.

        You don't seem to understand the explanation given. Please show your HTML. For debugging I suggest you look at the CGI object like this which will confirm the data received:

        #!/usr/bin/perl use CGI; use Data::Dumper; my $q = new CGI; print $q->header(), '<pre>', $q->escapeHTML(Dumper $q), '</pre>'; # this will give you all the values of the checkboxes # called param_name that were checked separated by spaces print join ' ', $q->param('param_name');

        This will show you exactly what your script is getting.

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print