Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: CGI.pm being funny?

by Zaxo (Archbishop)
on Feb 06, 2002 at 03:14 UTC ( #143543=note: print w/ replies, xml ) Need Help??


in reply to CGI.pm checkbox problem?

Among the other properties of &CGI::checkbox_group is {linebreak=>'true'}. The list of @predefined should be an arrayref:

print $q->checkbox_group ( -name=>'predefined', -values=>\@predefined, -linebreak=>'true' -default=>[ @predefined[@slicer] ], -labels=>\%predefined_labelhash, );
Those are all documented in 'perldoc CGI'.

Update: Corrected the omitted '-' in the arguments. Here is a working test:

#!/usr/bin/perl -w use strict; use CGI; my $q = CGI->new(); my @predef = ( 'abba', 'dabba', 'doo'); my @slicer = ( 1 ); my %labels = ( abba => 'pop', dabba => 'amateur', doo => "don't go the +re" ); print $q->checkbox_group ( -name => 'predefined', -values => \@predef, -default => [@predef[@slicer]],  -linebreak => 'true', -labels => \%labels,); =pod comment $ perl ~/tcgi.pl <input type="checkbox" name="predefined" value="abba" />pop<br /><inpu +t type="checkbox" name="predefined" value="dabba" checked="checked" / +>amateur<br /><input type="checkbox" name="predefined" value="doo" /> +don&#39;t go there<br /> $ =cut
xtype, maybe your @predefined array doesn't contain what you think it does.

After Compline,
Zaxo


Comment on Re: CGI.pm being funny?
Select or Download Code
Re: Re: CGI.pm being funny?
by xtype (Deacon) on Feb 06, 2002 at 05:16 UTC
    Actually,
    print $q->checkbox_group ( -name=>'predefined', -linebreak=>'true', -values=>\@predefined, -defaults=>param('predefined'), );
    does not appear to work. And instead fills the value and label of one checkbox with the arrayref. It returns something like:
    <input type="checkbox" name="predefined" value="ARRAY(0x833ced0)" />AR +RAY(0x833ced0)<br />
    Where as:
    print $q->checkbox_group ( -name=>'predefined', -linebreak=>'true', -values=>@predefined, -defaults=>param('predefined'), );
    Works perfectly well. And acts exactly the same as changing $, did. (except xhtml-ish br tag).
    <input type="checkbox" name="predefined" value="element content" />ele +ment content<br />
    The perldoc appears to say one thing and do another.
    Even in its' own examples...
    "...the second argument should be an array reference." and yet:
    print $query->checkbox_group(-name=>'group_name', -values=>['eenie','meenie','min +ie','moe'], -default=>['eenie','moe'], -linebreak=>'true', -labels=>\%labels);
    The perldoc also gives this example:
    @h = $query->checkbox_group(-name=>'group_name',-values=>\@values); &use_in_creative_way(@h);
    But this just fills @h with one element.
    print $h[0]; --- <input type="checkbox" name="group_name" value="ARRAY(0x833d000)" />AR +RAY(0x833d000)
    Very odd indeed.
      The reason for the behavior you are seeing is that @predefined contains an array reference, rather than a plain value. Since you don't show how you defined @predefined, it is impossible to say where you went wrong. Suffice it to say that:
      print $q->checkbox_group ( -name=>'predefined', -linebreak=>'true', -values=>@predefined, -defaults=>param('predefined'), );
      is not the right way to pass values to a CGI method. @predefined will be flattened into the argument list. If @predefined contains a single value, you've lucked out and it will work okay. If @predefined contains an odd number of values, the first will be taken as the value for the -values key, and the rest will be taken as separate key/value pairs. Worst case, if @predefined contains an even number of values, all the remaining explicit key/value pairs will be shifted by one, so that the keys are used as values and vice versa.
        I am splitting a "text" field in a database by newline.
        Each line is a different question.
        Each question is an element in @predefined. currently there are 30 elements.
        Everythings prints fine, and all values/labels match up.
        Just to test your odd vs. even theory, I added a question (new line to the text field).
        Everything still matches up. No shifting, no nastiness. Everythings works.
        Don't know what to say.

        update: Zaxo maybe, or maybe I am just crazy, or .. yeah, maybe. hrmph!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://143543]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2014-08-02 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (53 votes), past polls