問題描述
我想獲取我在 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 控制器中一系列復選框的值(如果選中)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!