php 如何获取 Laravel 4 控制器中一系列复选框的值(如果选中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19235910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:04:49  来源:igfitidea点击:

How to get the values for a series of checkboxes in Laravel 4 controller (if checked)

phpcheckboxlaravellaravel-4

提问by user1072337

I would like to get the values for a series of checkboxes I have set up in a Laravel 4 form. Here is the code in the view setting up the checkboxes:

我想获取我在 Laravel 4 表单中设置的一系列复选框的值。这是视图中设置复选框的代码:

@foreach ($friends as $friend)
<input tabindex="1" type="checkbox" name="friend[]" id="{{$friend}}" value="{{$friend}}">
@endforeach

In my controller, I would like to get the values for the checked boxes and put them in an array. I am not exactly sure how to do this, but I assume it is something like:

在我的控制器中,我想获取复选框的值并将它们放入一个数组中。我不完全确定如何做到这一点,但我认为它是这样的:

array[];

foreach($friend as $x)
if (isset(Input::get('friend')) {
        array[] = Input::get('friend');

 } 
endforeach

Could you provide me with a solution to do this? Thank you.

你能给我提供一个解决方案来做到这一点吗?谢谢你。

EDIT:

编辑:

This is what I have in the controller:

这是我在控制器中的内容:

public function describe_favorite() {

            $fan = Fan::find(Auth::user()->id);
            $fan->favorite_venue = Input::get('venue');
            $fan->favorite_experience = Input::get('experience');

            $friends_checked = Input::get('friend[]');

            print_r($friends_checked);

            if(is_array($friends_checked))
            {
             $fan->experience_friends = 5;
            }

            $fan->save();


            return Redirect::to('fans/home');

        }

It is not going through the "if" loop. How do I see the output of the print_r to see what's in the $friends_checked variable?

它没有通过“if”循环。如何查看 print_r 的输出以查看 $friends_checked 变量中的内容?

回答by Glad To Help

If checkboxes are related then you should use [] in the name attribute.

如果复选框是相关的,那么您应该在 name 属性中使用 []。

@foreach ($friends as $friend)
<input tabindex="1" type="checkbox" name="friend[]" id="{{$friend}}" value="{{$friend}}">
@endforeach


$friends_checked = Input::get('friend');
if(is_array($friends_checked))
{
   // do stuff with checked friends
}

回答by esifis

The array friend must have a key . If there is $friend->id you could try something like this.

数组朋友必须有一个 key 。如果有 $friend->id 你可以尝试这样的事情。

 @foreach ($friends as $friend)
  <input tabindex="1" type="checkbox" name="friend[{{$friend->id}}]" id="{{$friend}}">
 @endforeach

回答by JZILLA777

Using name="friend[]" in the form field creates an array named friend that is passed to the server, as opposed to name="friend" which passes a string value to the server.

在表单字段中使用 name="friend[]" 会创建一个名为 friend 的数组,该数组将传递给服务器,而不是 name="friend" 将字符串值传递给服务器。