Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery chosen plugin - get data using php

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?

like image 566
Onimusha Avatar asked Sep 06 '12 10:09

Onimusha


2 Answers

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

like image 131
Praveen Kumar Purushothaman Avatar answered Mar 16 '23 05:03

Praveen Kumar Purushothaman


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.

like image 22
igasparetto Avatar answered Mar 16 '23 03:03

igasparetto