dsgtew3241 2018-04-17 06:58
浏览 211
已采纳

如果复选框位于foreach循环中,如何存储复选框值(laravel)

I have List of the question and All question is print in a foreach loop. I want to store some question id in the database using checkbox.

@foreach ($allquestion as $indexKey => $all )
          <tr>
            <th>{{$indexKey+1}}</th>
            <th>{{substr($all->question,0,20)}} {{ strlen($all->question)>20 
             ? "..." : ""}}</th>
            <td>{{$all->option1}}</td>
            <td>{{$all->option2}}</td>
            <td>{{$all->option3}}</td>
            <td>{{$all->option4}}</td>
            <td>{{$all->answer}}</td>




            <td>
               <div class="form-check">
                 <input class="form-check-input big-checkbox" 
            name="present[]" type="checkbox" value="{{$all->id}}"   
          id="defaultCheck1">
              </div>
           </td>

          </tr>  @endforeach

I have no idea where I put submit button and how to get value from a checkbox into Controller to save data.

updated

{!! Form::open(array('route' => 'addq', 'data-parsley-validate' => '', 
'files' => true,'method'=>'post')) !!}
 @foreach ($allquestion as $indexKey => $all )
          <tr>
            <th>{{$indexKey+1}}</th>
            <th>{{substr($all->question,0,20)}} {{ strlen($all->question)>20 ? "..." : ""}}</th>
            <td>{{$all->option1}}</td>
            <td>{{$all->option2}}</td>
            <td>{{$all->option3}}</td>
            <td>{{$all->option4}}</td>
            <td>{{$all->answer}}</td>




            <td>
               <div class="form-check">
           <input type="checkbox" class="form-check-input big-checkbox"  
 name="checked[]" value="{{ $all->id }}">
              </div>
           </td>
          </tr>

      @endforeach
    {{Form::submit('Create Test',['class'=>'btn btn-primary'])}}
    {!! Form::close() !!}

When I Put Button like this no action perform.

  • 写回答

2条回答 默认 最新

  • doushi2902 2018-04-17 07:10
    关注

    The same functionality I implemented for one my project and it's working fine,please check the below code. I think it will be useful for you.

    HTML:

    <input type="checkbox" name="checked[]" value="{{ $employee->id }}">
    

    PHP:

    public function store(Request $request)
     {
       foreach ($request->employee_id as $key => $val) 
        {
         $payrolls = new Payroll;
          if (in_array($val, $request->checked))
           {
             $payrolls->basic = $request->basic[$key];
             $payrolls->employee_id = $val;
             $payrolls->save();
           }
        }
    return redirect('/');
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?