2014-05-22 3 views
0

사용자가 선택한 정보를 수집하는 형식의 체크리스트가 있습니다. 사용자가 선택한 확인란의 값을 가져 오려고합니다.매번 모든 확인란의 값을 반환합니다.

여기 여기 컨트롤러 부분

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

경로 여기

Route::get('addoption', array('as' => 'getaddoption', 'uses' => '[email protected]')); 
Route::post('addoption', array('as' => 'postaddoption', 'uses' => '[email protected]')); 

것입니다 확인란

@foreach($existing_options as $existing_option) 
    {{Form::label($existing_option, $existing_option)}} 
    {{Form::checkbox($existing_option, $existing_option)}} 
@endforeach 

그래서 다루는 형태 (dashboard.addcategory보기) 부분입니다 확인란이 만들어집니다. 이제 사용자가 선택한 확인란의 값을 알고 싶습니다.

나는 postAddOption 방법

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

를 통해이 양식을 처리하고 있습니다 그러나 그것은 나에게 모든 체크 박스의 배열을주고 있어요.

답변

0

모든 확인란의 이름이 동일해야하고 []이 붙어야합니다. 당신이 제출할 때

@foreach($existing_options as $existing_option) 
    {{ Form::checkbox('options[]', $existing_option) }} 
@endforeach 

는 지금, 그것은 당신이 그들을 통해 루프가 필요하지 않도록 해당 검사 체크 박스의 값의 각각의 값으로 채워집니다 배열로 제출됩니다.

if($validator->passes()){ 
    dd(Input::get('options')); 
} 
관련 문제