【问题标题】:How to store checkbox values if check box is in foreach loop (laravel)如果复选框在foreach循环中,如何存储复选框值(laravel)
【发布时间】:2018-04-17 16:01:46
【问题描述】:

我有问题列表,所有问题都在 foreach 循环中打印。我想使用复选框在数据库中存储一些问题 ID。

@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

我不知道我将提交按钮放在哪里以及如何从复选框中获取值到控制器中以保存数据。

更新

{!! 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() !!}

当我像这样放置按钮时,不会执行任何操作。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    我为我的一个项目实现的相同功能并且运行良好,请检查以下代码。我认为它对你有用。

    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('/');
     }
    

    【讨论】:

    • 谢谢,Deepti,你能帮我在这段代码中添加提交按钮吗
    • @brij 是的,您可以添加 HTML 提交按钮。这有什么问题?
    • 当我在循环注意发生后放置按钮时以及当我在循环中放置一个按钮时。它显示每一行。
    • 当我将按钮放入循环中时,它会显示错误。 "为 foreach() 提供的参数无效"
    • 如果您收到错误“为 foreach() 提供的参数无效”,代码如下 foreach ($request-&gt;employee_id as $key =&gt; $val) 尝试像 foreach ($request-&gt;checked as $key =&gt; $val) 那样做
    【解决方案2】:

    你应该试试这个

    刀片文件

    <form action="Your route">
    @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
    <button type="submit" class="btn btn-primary save">Submit</button>
    </form>
    

    在控制器中:

    $present = $request->get('present');
    dd($present);
    

    【讨论】:

      猜你喜欢
      • 2018-09-03
      • 2020-05-01
      • 1970-01-01
      • 2021-08-23
      • 2021-06-24
      • 2021-01-10
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      相关资源
      最近更新 更多