Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Set default in scrolling list

by hakkr (Chaplain)
on Jan 09, 2002 at 19:48 UTC ( [id://137467]=perlquestion: print w/replies, xml ) Need Help??

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

I want to set the default value for a scolling list using the contents of an array. I know this should be an easy matter of passing an array ref but it has me flumoxed. When I pass an array with more than 1 value the below code does not work. When I take out the -default altogether a default value still gets set but again only if the country param has one value. Even when I hard code in arrays with more than one value i.e ['gb','ie'] it don't work unless my script is passed 0 params <\p> This is so weird I think I'm doing something really stupid. I'm using CGI::Application and HTML:: Template if it makes any differance.

my $q = $self->query(); my @countrys=$q->param('country'); &CGI::scrolling_list( -name =>'country', -values=>[@CountryCodes], -default=>[@countrys], #fix -size=>5, -multiple=>'true', -labels=>$hashref );

Replies are listed 'Best First'.
Re: Set default in scrolling list
by chipmunk (Parson) on Jan 09, 2002 at 20:50 UTC
    Works fine for me. Note that, since CGI's form fields are sticky, you don't even need to set the default value. It will just pick up the current value(s) for the 'country' parameter anyway.

    If you really want to change the a form field's initial values, the docs for CGI explain how:

    Another note The default values that you specify for the forms are only used the first time the script is invoked (when there is no query string). On subsequent invocations of the script (when there is a query string), the former values are used even if they are blank. If you want to change the value of a field from its previous value, you have two choices: (1) call the param() method to set it. (2) use the -override (alias -force) parameter (a new feature in version 2.15). This forces the default value to be used, regardless of the previous value: print $query->textfield(-name=>'field_name', -default=>'starting value', -override=>1, -size=>50, -maxlength=>80);
Re: Set default in scrolling list
by Biker (Priest) on Jan 09, 2002 at 19:59 UTC

    I'd say: Working as designed.

    The '-default' is a value to be 'automatically' selected by the browser in the scrolling list. It is supposed to be one of the values found in the '-values'. I.e., give a scalar value as default. The value that you want to be selected by default.

    "Livet är hårt" sa bonden.
    "Grymt" sa grisen...

      I want to set multiple defaults. How can I pass a scalar with multiple values. The default I am setting is definitely a member of values.

      Update: Many thanks to chipmunk for -override=>1
      I was unaware of the stickyness as was the person who originally wrote the code.
      Thank you for returning my sanity

Re: Set default in scrolling list
by Masem (Monsignor) on Jan 09, 2002 at 20:12 UTC
    Not solving your problem but to generate an array ref given an array, prepend the variable with a '\' to get the reference to a variable. EG:
    -values => \@CountryCodes,
    (See perlref and some of the more recent reference Tutorials that have been posted here to get a handle for using refs).

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://137467]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found