如何从未选中的复选框Laravel

时间:2018-05-30 00:37:02

标签: php laravel checkbox input

我遇到了一些问题并寻求帮助。 有一个问题列表,用户应该回答它们。在请求中,我需要知道用户是否选择了答案。数组应包含类似

的内容

问题[0] => 0 //未选中

问题[1] => 1 //已选中

问题[2] => 1 //已选中

问题[3] => 0 //未选中

<div class="container">
    <div class="card m-1">
        <form action="{{action('PassquizzeController@send_ansers')}}" method="POST">
        @foreach ($quiz_questions as $question)
            <div class="card m-3 p-2">
                <h3>{{$question->question}}</h3>
                @foreach ($question->answers as $answer)
                    <div class="checkbox">
                        <label><input type="checkbox" name="question[{{$question->id}}]" value="{{$answer->id}}"><span class="blockquote"> {{$loop->index}}. {{$answer->answer}}</span></label>
                    </div>
                @endforeach
            </div>
        @endforeach
            {{ csrf_field() }}
        <button type="submit" class="btn btn-outline-success btn-lg btn-block">Send answers</button>
        </form>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

非常简单在复选框之前添加隐藏字段,然后替换下面的代码

抱歉合法的代码不好但我希望它能解决你的问题:

<div class="container">
    <div class="card m-1">
        <form action="{{action('PassquizzeController@send_ansers')}}" method="POST">
            @foreach ($quiz_questions as $question)
                <div class="card m-3 p-2">
                <h3>{{$question->question}}</h3>
                    @foreach ($question->answers as $answer)
                        <div class="checkbox">
                            <label>
                                <input type="hidden" name="question[{{$question->id}}]" value="0">
                                <input type="checkbox" name="question[{{$question->id}}]" value="{{$answer->id}}">
                                <span class="blockquote"> 
                                    {{$loop->index}}. {{$answer->answer}}
                                </span>
                           </label>
                       </div>
                      @endforeach
                    </div>
              @endforeach
              @csrf()

             <button type="submit" class="btn btn-outline-success btn-lg btn-block">Send answers</button>
           </form>
          </div>
          </div>