如何获取 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 控制器中一系列复选框的值(如果选中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Subtract time in PHP(在 PHP 中减去时间)
Limit execution time of an function or command PHP(限制函数或命令 PHP 的执行时间)
How to sum N number of time (HH:MM Format)?(如何求和 N 次(HH:MM 格式)?)
How to get current time in milliseconds in PHP?(如何在 PHP 中以毫秒为单位获取当前时间?)
How to check if time is between two times in PHP(如何检查时间是否在 PHP 中的两次之间)
Convert number of minutes into hours amp; minutes using PHP(将分钟数转换为小时数分钟使用 PHP)