Widget development – Drop down options won’t save

So, i successfully managed to add a drop down option, the problem is when i click save all of the options are removed, however the chosen option is implemented successfully.

Here is the relevant code:

Read More
function widget($args, $instance) {
        extract( $args );

        $pfxattribution = $instance['pfx-attribution'];
}

<p>
            <label for="<?php echo $this->get_field_id('pfx-attribution'); ?>"><?php _e('Choose attribution:'); ?></label>
            <select id="<?php echo $this->get_field_id('pfx-attribution'); ?>" name="<?php echo $this->get_field_name('pfx-attribution'); ?>" class="widefat" /> 
            <option>Purefx</option>
            <option>Foreign Exchange</option>
            <option>Currency Exchange</option>
            </select>
            </p>

function update($new_instance, $old_instance) {
        // Get the old values
        $instance = $old_instance;

        // Update with any new values (and sanitise input)
        $instance['pfx-attribution'] = strip_tags( $new_instance['pfx-attribution'] );

       return $instance;
}

Any help is greatly appreciated

Related posts

Leave a Reply

3 comments

  1. Danny, this is pretty much lifted straight from a plug-in I’ve made:

     <?php function form($instance){    
        $instance = wp_parse_args( (array) $instance, $this->w_arg );
      ?>
      <p>
        <select id="<?php echo $this->get_field_id('order'); ?>" name="<?php echo $this->get_field_name('order'); ?>" type="text">
            <option value="asc" <?php selected($instance['order'], 'asc'); ?>>ASC </option>
            <option value="desc" <?php selected($instance['order'], 'desc');?>>DESC </option>
        </select>
    </p>
    
    <?php
      }
    ?>
    

    Without seeing the rest of your code I can’t be sure the root of the problem, but it could be the form is not wrapped inside the form function. If the above doesn’t seem to work for you, perhaps pastie your Widget Class?

    I should perhaps add that my widget class has a variable w_arg, an array of defaults! E.g.

    var $w_arg = array(
            'order'=> 'ASC'
            );