Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://164810]
Approved by boo_radley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2023-03-31 06:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (75 votes). Check out past polls.

    Notices?