Home » Php » javascript – Values submitted by a form with `multiple` in `bootstrap-select`

javascript – Values submitted by a form with `multiple` in `bootstrap-select`

Posted by: admin July 12, 2020 Leave a comment

Questions:

If one generates a form with bootstrap and bootstrap-select:

<form action="handle.php" method="post">
    <select name="pids" class="selectpicker" multiple>
        <option value="1">A</option>
        <option value="2">B</option>
        <option value="6">C</option>
    </select>
</form>

And one select all or multiple items, only the item with the last selected value is posted to the handle.php page.

handle.php

<?php
var_dump($_POST);
?>

result:

array(1) { ["pids"]=> string(1) "6" } 

how can one retrieve all selected items?

How to&Answers:

Its most likely a case that you need to set the name to pids[] (note the square brackets) The square brackets define an array instead of a single value.

<select name="pids[]" class="selectpicker" multiple>

Answer:

<select name="pids[]" class="selectpicker" multiple>

Iam not sure but something like that 🙂

Answer:

Just a quick note…even though this one is accepted since two years…

For this to work you must POST to a file (which makes it reload). I tried figuring it out for 5 hours.

But, if you want to use Ajax…then you can send all values by

var values_from_list=$('#id_of_the_field').val();

and it will send all to the little ajax array,
and in PHP you can json_encode it and save it to your db.

Hope it saves someone time.