dousi4900 2013-10-07 22:30
浏览 140
已采纳

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

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:

@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?

  • 写回答

3条回答 默认 最新

  • dongnvwang8591 2013-10-08 01:12
    关注

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

    @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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效