11

I'm using Multiple Select with Groups and Multiple Select from http://harvesthq.github.com/chosen/

Does anyone know how to get the values of the submitted data using php?

$_POST['countries']; does not work and only returns one of the selected items (not an array)

And here is the select tag name/id etc

 <select name="countries" multiple="multiple" class="chzn-select" id="countries" tabindex="6" data-placeholder="Countries">

PS. I already checked Chosen Jquery Plugin - getting selected values but can't figure out what to do except for getting the value. Is there a straight forward method without using events to update a hidden field and submit data?

Community
  • 1
  • 1
Onimusha
  • 3,348
  • 2
  • 26
  • 32

2 Answers2

18

When you are posting multiple values using a <select> you need to name it a way, it looks like an array.

<select name="countries[]">
</select>

This should be the way. And upon POST, the variable should be accessible as:

$_POST['countries'] = array(
  [0] => 'India' , // First selected value
  [1] => 'Indiana' , // Second
  [2] => 'USA' // Third
);

// So...
echo $_POST['countries'][1];
// would print "Indiana"

Note: The values will be held in a standard, zero-indexed array

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
4

Check these links: http://www.onlinetools.org/tricks/using_multiple_select.php and How to get multiple selected values of select box in php?

The select name should have square brackets at it's end, so it would be

name="countries[]"

Use print_r() at the PHP code to see what was posted. Use foreach() to loop through the values.

Community
  • 1
  • 1
igasparetto
  • 1,086
  • 1
  • 12
  • 28