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条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件