如何创建评分帖子?

时间:2019-10-08 08:40:23

标签: php laravel

我有一个课程列表,每个课程的旁边是input type = "radio",用于从1到10分对该课程进行评估。我要单击单选按钮,将获取该按钮的值并将其保存到数据库。但这似乎不起作用。

<tbody>
   @foreach($rates as $rate)
   <form method="post" action="{{url('/rating')}}">
   <input type="hidden" name="rateId" value="{{$rate->id}}">
      {{ csrf_field() }}
        <tr style="display: flex;">
          <td class="col-md-1"></td>
          <td class="col-md-6" style="text-align: left;"><a href="{{url('/bai-giang/'.$rate->id)}}">{{$rate->title}}</a></td>
          <td class="col-md-3" style="text-align: center;"></td>
          <td class="col-md-2">
          <div align="center" style="display: inline-flex;">
          <input type="radio" name="rating" value="1" onclick="return confirmAction()">
          <input type="radio" name="rating" value="2" onclick="return confirmAction()">
          <input type="radio" name="rating" value="3" onclick="return confirmAction()">
          <input type="radio" name="rating" value="4" onclick="return confirmAction()">
          <input type="radio" name="rating" value="5" onclick="return confirmAction()">
          <input type="radio" name="rating" value="6" onclick="return confirmAction()">
          <input type="radio" name="rating" value="7" onclick="return confirmAction()">
          <input type="radio" name="rating" value="8" onclick="return confirmAction()">
          <input type="radio" name="rating" value="9" onclick="return confirmAction()">
         <input type="radio" name="rating" value="10" onclick="return confirmAction()">
          </div>
       </td>
  </tr>
</form>
@endforeach
</tbody>

我保存表单值的功能


    public function rating(Request $req)
        {
            $id = $req->rateId;
            $lesson = Lesson::find($id);
            $lesson->rating = $req->rating;
            $lesson->save();
            return redirect()->back()->with('success', 'Rating successful');
        }

1 个答案:

答案 0 :(得分:0)

对于所有单选按钮,name标记中的input属性应该相同,并且在获取所选值时应使用该名称。

示例:

<input type="radio" name="rating" value="3" onclick="return confirmAction()">
<input type="radio" name="rating" value="4" onclick="return confirmAction()">