Laravel如何通过单击按钮在<li>中获取价值并将值发送到Controller

时间:2017-11-12 19:20:34

标签: javascript php html laravel

对于每个学生,我需要在我的HTML中输出他的ID和按钮,

 <form enctype="multipart/form-data" id="findStudent" action="{{url('student/find')}}" method="POST">
          <ul>
            @foreach ($students as $student)
            <li>ID: {{ $student->sid }}<br></li>
            <div>
            <input id = "finding" type="submit"  value="Find Student" />
            </div> <br/>
          @endforeach
          </ul>
   </form>

我想获得sid的值,我点击了控制器中的相应按钮:

public function find(Request $find){    
        $sid = $find->input('student');
        var_dump($sid);
}

但是我得到了NULL,问题是什么? 谢谢,问题解决了。

但如果我有其他功能

   public function request(Request $request){

        $teacher_name = $request->input('teacher_name');

}

我想在找到$ teacher_name,我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

您可以使用<button>元素代替input元素:

<form enctype="multipart/form-data" id="findStudent" action="{{url('student/find')}}" method="POST">
    <ul>
    @foreach ($students as $student)
        <li>ID: {{ $student->sid }}<br></li>
        <div>
            <button name="student" type="submit" value="{{ $student->sid }}">
                Find Student
            </button>
        </div>
        <br/>
    @endforeach
    </ul>
</form>
  

参考:https://www.w3schools.com/tags/att_button_value.asp

此外,如果您想使用元素ID,您必须通过将学生ID连接到它来使其唯一:

<button id="finding{{ $student->sid }}" name="student" type="submit" value="{{ $student->sid }}">