Site Overlay

Retrieving selections from a multiselect Select Field with select2.js

Recently I needed to add a multi-select field to a project. I used the Select2JS library to achieve just that.

Just to always remember, you can retrieve the selection with PHP (form handler) by setting the field’s name attribute to an array, e.g.:

<select name="myFieldValues[]">

Then on the PHP form handler file you can catch the fields as follows:

$valuesArray = $_POST['myFieldValues'];

Do a var_dump of the array so you can see the array.

echo '<pre>'.

An array of values ‘PHP’, ‘HTML’, ‘NODEJS’, ‘WORDPRESS’, ‘WORDPRESS HOOKS’, should display as follows:

Array display

Leave a Reply

Your email address will not be published. Required fields are marked *