Php Laravel每次返回所有复选框的值
我有一个表单中的检查表,它从用户那里收集一些信息。我试图获取用户选择的那些复选框的值 这是路线Php Laravel每次返回所有复选框的值,php,checkbox,laravel,Php,Checkbox,Laravel,我有一个表单中的检查表,它从用户那里收集一些信息。我试图获取用户选择的那些复选框的值 这是路线 Route::get('addoption', array('as' => 'getaddoption', 'uses' => 'CategoryController@getAddOption')); Route::post('addoption', array('as' => 'postaddoption', 'uses' => 'CategoryController@pos
Route::get('addoption', array('as' => 'getaddoption', 'uses' => 'CategoryController@getAddOption'));
Route::post('addoption', array('as' => 'postaddoption', 'uses' => 'CategoryController@postAddOption'));
这是控制器部分
public function getCategoryForm(){
//$this->option is a model interface which deals with table named 'options' having one column 'option'
$existing_options = $this->option->lists('option');
return View::make('dashboard.addcategoryform')->with('existing_options', $existing_options);
}
下面是表单中处理复选框的部分(在dashboard.addcategory视图中)
@foreach($existing_options as $existing_option)
{{Form::label($existing_option, $existing_option)}}
{{Form::checkbox($existing_option, $existing_option)}}
@endforeach
这样就创建了复选框。现在我想知道用户选择的复选框的值
我正在通过邮寄方式处理此表格
if($validator->passes()){
$existing_option = $this->option->lists('option');
foreach($existing_option as $existing_opt){
if(Input::get($existing_opt) == true){
$selected_option[] = $existing_opt;
}
}
print_r($selected_option);
}
但它给了我一个所有复选框的数组。所有复选框都应该有相同的名称,并附加
[]
@foreach($existing_options as $existing_option)
{{ Form::checkbox('options[]', $existing_option) }}
@endforeach
现在,当您提交时,它将作为一个数组提交,该数组将填充相应复选框值的每个值,这样您就不需要遍历它们
if($validator->passes()){
dd(Input::get('options'));
}