Re: [htmltmpl] Select/option How to set "selected"?
Brought to you by:
samtregar
From: Jason P. <ja...@jo...> - 2004-05-03 21:37:01
|
Hi Mark, How are you determining which option to pre-select? It might be better to use HTML::FillInForm. Other than that, here's what you would do if you want to re-invent the wheel: ### In your programming code ### my $template = HTML::Template->new( 'filename' => 'file.TMPL' ); my $occloop_ar = []; # occupation loop array ref. while ( my ( $option, $value ) = $sth->fetchrow_array ) { my $selected = 0; $selected = 1 if i_want_this_option_selected( $option ); push @$array_ref, { 'VAL' => $option, 'TEXT' => $value, 'SELECTED' => $selected, }; } $template->param( 'OCCUPATION_LOOP' => $occloop_ar ); ### Then in your template code ### Occupation: <select name="occupation"> <TMPL_LOOP NAME=OCCUPATION_LOOP> <option value="<TMPL_VAR NAME=VAL>" <TMPL_IF NAME="SELECTED">SELECTED</TMPL_IF>><TMPL_VAR NAME=TEXT></option> </TMPL_LOOP> Cheers, Jason |