沃梦达 / 编程问答 / php问题 / 正文

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

How to get the values for a series of checkboxes in Laravel 4 controller (if checked)(如何获取 Laravel 4 控制器中一系列复选框的值(如果选中))

本文介绍了如何获取 Laravel 4 控制器中一系列复选框的值(如果选中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取我在 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.

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

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');

        }

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

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?

推荐答案

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

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
}

这篇关于如何获取 Laravel 4 控制器中一系列复选框的值(如果选中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何获取 Laravel 4 控制器中一系列复选框的值(如

基础教程推荐