PHP ( Wordpress) - Select multiple - show "selected" state not working

admin

Administrator
Staff member
I can´t seem to get the multiple select to display the
Code:
selected = "selected"
attribute when saving or refreshing .

Everything works (db insert is OK, results output OK), but i can not get the selected values to show after refreshing .

I am trying to use the <a href="http://codex.wordpress.org/Function_Reference/selected" rel="nofollow">selected()</a> function from the wp core ..

The code (it is inside a widget class and serves for a fairly simple widget ) :

Code:
&lt;!-- List Custom Fields Hide Underscore (hide=no) --&gt;
 &lt;p&gt;
  &lt;?php $keys_no = $this-&gt;k99_cfsw_get_all_cf_array('no');?&gt;
   &lt;?php if ( $keys_no ) { ?&gt;

  &lt;label for="&lt;?php echo $this-&gt;get_field_id('cfl2'); ?&gt;"&gt;&lt;?php _e('Select your custom field','k99_gelosa_domain'); ?&gt;&lt;/label&gt; 

    &lt;select multiple="multiple" class="chzn-select" style="width:150px;height:29px;" size="5" id="&lt;?php echo $this-&gt;get_field_id('cfl2'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('cfl2'); ?&gt;[]" value="&lt;?php $instance['cfl2'] ?&gt;"&gt;
    &lt;option  value="&lt;?php isset($instance['cfl2']) ? _e($instance['cfl2']) : _e('#NONE#'); ?&gt;"&gt;&lt;?php isset($instance['cfl2']) ? _e('multiple') /*_e($instance['cfl2'])*/ : _e('#NONE#'); ?&gt;&lt;/option&gt;

    &lt;!--&lt;option value="#NONE#"&gt;&lt;?php //echo $cfl; ?&gt;&lt;/option&gt;--&gt;

    &lt;!--&lt;option value="------"&gt;-------&lt;/option&gt;--&gt;
    &lt;?php
        foreach ( $keys_no as $key ) {
            echo "\n&lt;option value='" .esc_attr($key) . "'". selected( $instance['cfl2'],esc_attr($key) ,FALSE)."&gt;". esc_html($key) . "&lt;/option&gt;";
        }
    ?&gt;
    &lt;/select&gt;
    &lt;?php

} ?&gt;
&lt;/p&gt;

as a side note and a bonus question - , I am using also the <a href="http://harvesthq.github.com/chosen/" rel="nofollow">chosen JS</a> - it is also working ok, (except that it does not show the selected items ..) but have also a small bug that it will not initiate when the widget is closed - only after saving in an open state ...