How do I pass back multiple select box selections to HTML form? -


i using perl (and dancer) html create form. on form have fields so:

<input type="text" name="keywords" id="keywords" value="<% keywords | html %>"/>  <select multiple="multiple" id="sentences" name="sentences" size="3"/>     <% if list_of_sentences %>          <% foreach sentence in list_of_sentences %>               <option value=<% sentence %>/>          <% end %>     <% end %> </select> 

these are, of course, inside own divisions , there more code around, these essentials of i've got.

these in main.tt interacted using myapp.pm , there parameters passed , forth (e.g. keywords , list_of_sentences here). code set 2 divs, 1 form , other purely show output. when form submitted output shows correctly, select field selections not retained.

so therefore, want make chosen selection(s) 'multiple select' kept selected.

as example, in 'input' keywords field have value retained because has been passed .pm file using param("keywords") using template 'main' .... works value="<% keywords | html %>" in html code. have tried same multiple select using param("sentences"), returns blank field.

to hazard guess @ problem because there more 1 option , maybe because getting options .pm file start with, haven't found solution yet, welcome.

you want use selected="selected" attribute on option element. suggest passing both name of sentence , flag stating if option should in selected state. example:

<% if list_of_sentences %>   <% foreach sentence in list_of_sentences %>     <option value="<% sentence.value %>"<% if sentence.is_selected %> selected="selected"<% end %> />   <% end %> <% end %> 

and use following data structure sentences:

{ value => $value, is_selected => 0|1 } 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -