【问题标题】:Passing form data inside html table to controller in laravel将html表中的表单数据传递给laravel中的控制器
【发布时间】:2019-06-28 16:26:47
【问题描述】:

我是 laravel 新手,我想将数组数据从 html 表传递到控制器

我已尝试使用此代码

控制器

foreach($request->input('chk', []) as $key => $chk){
   $att = new Attendance;
   $att->student_id = $key;
   $att->date = $request->att_day;
   $status = $chk == 'true' ? 1 : 0;
   $att->status = $status;

}

查看

<form action="{{route('attendance.save')}}" method="post" class="form-horizontal form-bordered">
<table class="table">
   <thead>
      <tr>
         <th class="text-center">#</th>
         <th>Student Name</th>
         <th>Status</th>
      </tr>
   </thead>
   <tbody>
   @foreach ($students as $key => $s)
      <tr>
         <td class="text-center">{{ $key + 1 }}</td>
         <td>{{ $s->student_name }}</td>
         <td>
            <div class="custom-control custom-radio radio-primary">
               <input type="radio" id="rdo_{{$key}}_pre" name="rdo[{{$s->id}}]" class="custom-control-input" checked>
               <label class="custom-control-label" for="rdo_{{$key}}_pre">Present</label>
            </div>
            <div class="custom-control custom-radio radio-pink">
               <input type="radio" id="rdo_{{$key}}_abs" name="rdo[{{$s->id}}]" class="custom-control-input">
               <label class="custom-control-label" for="rdo_{{$key}}_abs">Absent</label>
            </div>
         </td>
      </tr>
   @endforeach
   </tbody>
</table>
</form>

当我检查传递给控制器​​的数据时,根据表中的第一个数据,都是或。如何获取视图中选择的rdo值

【问题讨论】:

    标签: 1 php laravel


    【解决方案1】:

    您只需要为每个输入添加一个 value 属性来表示准确检查的内容。

    <td>
        <div class="custom-control custom-radio radio-primary">
            <input type="radio" id="rdo_{{$key}}_pre" name="rdo[{{$s->id}}]" class="custom-control-input" value="present" checked>
            <label class="custom-control-label" for="rdo_{{$key}}_pre">Present</label>
        </div>
        <div class="custom-control custom-radio radio-pink">
            <input type="radio" id="rdo_{{$key}}_abs" name="rdo[{{$s->id}}]" class="custom-control-input" value="absent">
            <label class="custom-control-label" for="rdo_{{$key}}_abs">Absent</label>
        </div>
    </td>
    

    【讨论】:

      【解决方案2】:

      尝试将foreach($request-&gt;input('chk', []) as $key =&gt; $chk) 更改为foreach($request-&gt;input('rdo') as $key =&gt; $chk)?

      【讨论】:

      • 为什么会这样? input() 的第二个参数如果不存在则只是默认值;省略它的原因是什么?为什么您认为这可以回答所提出的问题?
      猜你喜欢
      • 2019-01-06
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 2023-02-04
      • 2021-06-23
      • 1970-01-01
      相关资源
      最近更新 更多