Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

popup_menu default that isn't

by spork (Monk)
on May 07, 2002 at 20:19 UTC ( #164810=perlquestion: print w/replies, xml ) Need Help??
spork has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to create a popup menu in a CGI script with a default but I'm having difficulty with setting the default value. Here is my code:
@values = sort numerically keys %ProgramAreas; $default = $ProgramAreas{$ComponentAreas{$ComponentID}}; print '<TD ALIGN="right"><B>Program Area:</B></TD><TD ALIGN= +"left">', popup_menu( -name=> 'ComponentProgramArea', -values=> \@values, -defaults=> \$default, -labels=> \%ProgramAreas), "</TD><BR><BR><BR>\n";
ProgramAreas is a hash that is used in a few other places and I know that it contains useful and legal data.

$default gets assigned a real, legal value based on a print statement I threw in as a diagnostic. However, when the menu is displayed the default value is the first item in @values and not $default.

What obvious piece am I missing?

Replies are listed 'Best First'.
Re: popup_menu default that isn't
by Zaxo (Archbishop) on May 07, 2002 at 20:53 UTC

    The attribute is 'default', not 'defaults', and its value shourd be a string, not a reference to scalar. I suspect that you want the values from %ProgramAreas as labels:

    -default => $default, # -labels => [@ProgramAreas{@values}] ),
    In your sort, do you mean @values = sort {$a <=> $b} keys %ProgramAreas; ? What you have looks like pseudocode, or possibly Perl6 ;-)

    Update: Was wrong about labels.

    After Compline,
    Zaxo

Re: popup_menu default that isn't
by DigitalKitty (Parson) on May 07, 2002 at 20:53 UTC
    Hi Spork.

    Could you post some more of the source code? If not, you can set a default value with CGI.pm like this:
    print popup_menu(-name=>'food', -values=>['pasta','fish','chicken', 'vegetables','fruit','bread','dessert'], -default=>'dessert');


    I once had a problem with a cgi program I writing where the script wouldn't work until I changed the word 'values' to 'value' ( without the quotes ).

    If this reply doesn't provide a solution, just post again o.k?

    You can find some more perl/CGI tutorials at:

    perl.about.com

    and...

    devshed.com

    I would try a supersearch here first though.

    Hope this helps,

    -Caitlin

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://164810]
Approved by boo_radley
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2018-02-20 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (268 votes). Check out past polls.

    Notices?