The value for USERSTATE is being passed as normal, just like for any other single input box. The problem is that YOU have tell which item from the select box was selected. No matter how you go about it, you will have to loop through every item that will be in the select box and see if it is the one the user selected. If so, then you need pass a "flag" to let the template know to add the magic selected=1 attribute to the proper <option> tag.

And that's why a lot of folks simply let CGI.pm figure that out for them. When you use CGI.pm's popup_menu or scrolling_list methods, CGI.pm will mark the proper <option> tag with the selected=1 attribute. The following example uses Geography::States instead of a database. The first part of the code simply munges the results from Geography::States into a datastructure that is suitable for an HTML::Template <tmpl_loop> -- but instead i pass that off to CGI.pm's scrolling_list method, hence the maps. You can test this code out at [http://unlocalhost.com/cgi-bin/select.cgi]

use strict; use warnings; use Data::Dumper; use HTML::Template; use Geography::States; use CGI qw(:standard); my @state; my $usa = Geography::States->new('USA'); push @state,$_ for $usa->state; my $state = [ map {{ id => $_->[0], name => $_->[1] }} sort { $a->[1] cmp $b->[1] } @state ]; my $tmpl = HTML::Template->new(filehandle => \*DATA); $tmpl->param(states => scrolling_list( -name => 'states', -values => [ map $_->{id}, @$state ], -size => 12, -multiple => 'why not', -labels => { map {$_->{id} => $_->{name}} @$state }, ), ); print header,$tmpl->output; __DATA__ <form> <tmpl_var states> <p><input type="submit" /></p> </form>
The other option is to loop through the select items yourself and mark which one was selected. Then you need a 'not-so-elegant' template like the following:
<select name="states"> <tmpl_loop states> <option value="<tmpl_var value>" <tmpl_if selected>selected="1 +"</tmpl_if> > <tmpl_var label> </option> </tmpl_loop> </select>
Since you are already using CGI.pm ... why not let it do the hard work of making the select boxes for you as well?

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to Re: HTML::Template, associate 'method', and popup menus by jeffa
in thread HTML::Template, associate 'method', and popup menus by geektron

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.