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

I'm using CGI.pm v 2.74 and I can't get my defaults to work with the popup_menu routine. I use this frequently, and it's been working. What I have here looks like the same syntax of the other places I use it where it works, so I'm very confused. Here's a simplified case:
$html .= popup_menu( -name=>'col_id', -values=>[1,2], -default=>2, -labels=>{1=>'Ali',2=>'Baba'}, );
It defaults to the first value instead of the second. What am I missing? Thanks, -mark

Replies are listed 'Best First'.
Re: CGI.pm popup_menu default problem
by cianoz (Friar) on Oct 05, 2000 at 21:57 UTC
    maybe col_id allready has a value:
    try to put
    $query->delete('col_id');
    before my $html .= popup_menu ...
Re: CGI.pm popup_menu default problem
by AgentM (Curate) on Oct 05, 2000 at 21:16 UTC
    Your problem is most likely in a previous line where you are appending your $html var. Why are you doing this anyway? Oh well, your business. But an unclean HTML tag may be causing you problems since this looks OK. Does anyone else see a problem? This looks like a contextual HTML interpreter error due to bad HTML tags .
    AgentM Systems or Nasca Enterprises is not responsible for the comments made by AgentM- anywhere.
      I think AgentM is right. I went to test this code:

      use strict; use CGI qw(:standard); my $html .= popup_menu( -name=>'col_id', -values=>[1,2], -default=>2, -labels=>{1=>'Ali',2=>'Baba'}, ); print $html;

      And here is the output I got:

      <SELECT NAME="col_id"> <OPTION VALUE="1">Ali <OPTION SELECTED VALUE="2">Baba </SELECT>
      I assume this is what you wanted so the error may not be in the bit of code you provided.

      Zenon Zabinski | zdog | zdog7@hotmail.com

RE: CGI.pm popup_menu default problem
by merlyn (Sage) on Oct 06, 2000 at 05:56 UTC
    Just a wild guess, but...
    Is this on a second invocation? Remember, because of sticky fields, the -default is valid only on the first invocation, so an incoming field of col_id of 2 on a second invocation will influence the "default".

    If you don't want that behavior, set -override => 1 or issue a param('col_id', undef) before you generate the HTML.

    -- Randal L. Schwartz, Perl hacker

Re: CGI.pm popup_menu default problem
by markjugg (Curate) on Oct 06, 2000 at 00:37 UTC
    I just tried to run my test snippet in a "clean" test script and got the correct result of having "Baba" selected. However, in my Real World Script, it fails. BUT, if I change the 'name' attribute to 'col_foo_id', it works. This seems to support cianoz's theory above, but there are two reasons this doesn't make sense:

    1. I'm using the procedural interface, and 'col_id' is not not even declared with CGI.pm, it's simply appended to the HTML variable.

    2. The actual name I'm using is 'col_manufacturer_id', which does not appear elsewhere on the page.

    I'm still confused. Thanks for any other suggestions.

    -mark

Re: CGI.pm popup_menu default problem
by markjugg (Curate) on Oct 06, 2000 at 17:19 UTC
    I just tried to run my test snippet in a "clean" test script and got the correct result of having "Baba" selected. However, in my Real World Script, it fails. BUT, if I change the 'name' attribute to 'col_foo_id', it works. This seems to support cianoz's theory above, but there are two reasons this doesn't make sense:

    1. I'm using the procedural interface, and 'col_id' is not not even declared with CGI.pm, it's simply appended to the HTML variable.

    2. The actual name I'm using is 'col_manufacturer_id', which does not appear elsewhere on the page.

    I'm still confused. Thanks for any other suggestions.

    -mark