简体   繁体   English

如何从多选输入中获取所有选定的值?

[英]How to get all the selected values from a multiselect input?

I am using Laravel and I tried to used die(var_dump('$request->input('users')));我正在使用 Laravel 并尝试使用die(var_dump('$request->input('users'))); to check the data that are selected using a multiselect input.检查使用多选输入选择的数据。 When I selected all 3 users, the output was just showing the last user id like this当我选择所有 3 个用户时,输出只是显示最后一个用户 ID,如下所示

array(1) {
  [0]=>
  string(1) "3"
}

This is the input.这是输入。 The name was set as user[0] because the inputs can be multiple depending on the user how many multiselect input is needed名称设置为user[0]因为输入可以是多个,这取决于用户需要多少个多选输入

<select class="form-control user" id="user" name="user[0]" multiple="">
    @foreach ($users as $user)
        @if (old('user') == $user->id)
            <option value="{{ $user->id }}" selected="">{{ ucwords($user->name) }}</option>
        @endif
    @endforeach
</select>

And this below if from the controller下面这个如果来自控制器

$users = array();
$user = $request->input('user');
$user = implode(',', $user);

You should have the input name setup as the syntax for adding an element to an array:您应该将输入名称设置为将元素添加到数组的语法:

 name="user[]"

Then you will get your array from $request->input('user', []);然后你会从$request->input('user', []);得到你的数组$request->input('user', []);

If you are going to have multiple different selects you can use the index in them if you wish:如果您有多个不同的选择,您可以根据需要在其中使用索引:

 name="user[0][]"
 name="user[1][]"

You have make your input field as array.您已将输入字段设为数组。

blade.php file刀片.php文件

<select class="form-control user" id="user" name="user[]" multiple="">
    @foreach ($users as $user)
        @if (old('user') == $user->id)
            <option value="{{ $user->id }}" selected="">{{ ucwords($user->name) }}</option>
        @endif
    @endforeach

</select>

controller控制器

$users = array();
$users = $request->input('user',[]);
$users = implode(',', $users);

Why are you converted array into string here $users = implode(',', $users);为什么在这里将数组转换为字符串$users = implode(',', $users); ? ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在发布/获取中获取多选下拉列表值的所有选定值? - How to get all the selected values of multiselect dropdown value in post/get? 使用PHP从引导多重选择中获取所有选定值? - Get all selected values from bootstrap multiselect using PHP? 如何从bootstrap multiselect中获取所有选定数据的总和并显示在其他输入中? - How to get sum of all selected data from bootstrap multiselect and show in anoher input? 如何从yii2中的多选复选框输入中选择用户? - how to get user selected from multiselect checkbox input in yii2? 如何在多选表单上从数据库中回显多选中的选定值 - How to echo selected values on multiselect from database on a multiselect form 如何在For循环下获取选择的Multiselect选定值 - How to get Chosen Multiselect selected values under the For Loop 如何从laravel中的多选返回选定值的列表? - How to return the list of selected values from multiselect in laravel? Codeigniter获取选定的form_multiselect值 - Codeigniter get selected values of form_multiselect 在多选表单字段中获取选定的值 - Get selected values in multiselect form field 每个窗体多个MultiSelect Drowdowns - 如何一般地获取所有值 - Multiple MultiSelect Drowdowns Per Form - how to get all the values generically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM