Make WordPress Core

Opened 13 years ago

Closed 13 years ago

#20679 closed defect (bug) (worksforme)

Widget with mutliple select doesn't work

Reported by: topweb's profile topweb Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.3.2
Component: Widgets Keywords: needs-patch
Focuses: Cc:

Description

Widget with <select multiple="multiple"> doesn't work.
To reproduce the bug you need to test the following code in a widget:


public function update($new_instance, $old_instance) {
    print_r($new_instance);
    die();
}

function form($instance) { ?>
        <select multiple="multiple" name="<?php echo $this->get_field_name('options'); ?>">
                <option value="1">
                Option 1
                </option>
                <option value="2">
                Option 2
                </option>
                </option>
        </select> <?php
} 

Change History (8)

#1 @dd32
13 years ago

Can you add some details of what doesn't work, and how you expect that it should work?

#2 @johnbillion
13 years ago

FWIW the multiple select element is a very poor element from a UI and UX perspective. I can guarantee you'll get users who don't know how to select multiple items in it. You might be better off replacing it with checkboxes which are much more widely used and understood.

#3 @topweb
13 years ago

Yes here are more details:

Post parameters when saving are:

action	save-widget
add_new	
id_base	featuredgames
multi_number	
savewidgets	b277827e4e
sidebar	left-sidebar-home-page
widget-featuredgames[2][categoriesIds]	7
widget-featuredgames[2][categoriesIds]	8
widget-height	200
widget-id	featuredgames-2
widget-width	250
widget_number	2

As you can see categoriesIds should be an array from the multiple select.

I am expecting to get from the update function an array containing both submitted values:

public function update($new_instance, $old_instance) {
     print_r($new_instance); 
     die();
}

And it actually returns this:
Array ( [categoriesIds] => 8 )

I agree, dd32, it maybe not the best UI option but still it it's the fastest way
to achieve it...

Last edited 13 years ago by topweb (previous) (diff)

#4 @jane
13 years ago

What johnbillion said. Do we have a widget with multiselect? If so, it's an oversight on my part or I would have booted it long ago for that reason. Checkboxes are the approved multi-select UI in the WP admin.

#5 @scribu
13 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

What Jane said. We do not use multiselect anywhere in WP and we should not encourage plugin authors to use them either. Checkboxes FTW.

#6 follow-up: @kurtpayne
13 years ago

  • Cc kpayne@… added

Again, checkboxes is the way to go. Just wanted to defend the core, though. The behavior you're seeing is because you need two square brackets after a multiple select field name to tell php to treat the field as an array.

<select multiple="multiple" name="<?php echo $this->get_field_name('options'); ?>[]">

#7 @johnbillion
13 years ago

  • Resolution wontfix deleted
  • Status changed from closed to reopened

#8 in reply to: ↑ 6 @johnbillion
13 years ago

  • Resolution set to worksforme
  • Status changed from reopened to closed

Replying to kurtpayne:

The behavior you're seeing is because you need two square brackets after a multiple select field name to tell php to treat the field as an array.

Confirmed.

Note: See TracTickets for help on using tickets.