仅检索多个select元素中的最后一个选项

时间:2015-03-20 17:35:38

标签: laravel laravel-4 eloquent

表单已发布,我正在检索输入:

$inputs = Input::all();
return Response::json($inputs);

表格是这样的:

<form role="form" method="POST" action="/account/enrollments/{{ $activity->id }}/update" class="form-horizontal" autocomplete="off">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <div class="row">
        <div class="col-xs-12 col-md-4">
                <h4>Available Students</h4>
            <select multiple class="form-control" name="available">
                @foreach ($available as $student)
                    <option value="{{ $student->id }}">{{ $student->firstname }} {{ $student->lastname }}</option>
                @endforeach
            </select>
        </div>

        <div class="col-xs-12 col-md-4 push-top">
            <button name="enrol" class="btn btn-block btn-effect-ripple btn-primary">Enrol Student(s)</button>
            <button name="unenrol" class="btn btn-block btn-effect-ripple btn-warning">Remove Student(s)</button>
        </div>

        <div class="col-xs-12 col-md-4">
                <h4>Currently Enrolled Students</h4>
            <select multiple class="form-control" name="enrolled">
                @foreach ($enrolled as $student)
                    <option value="{{ $student->id}}">{{ $student->firstname }} {{ $student->lastname }}</option>
                @endforeach
            </select>
        </div>
    </div>
</form>

但是响应只显示了最后一个选中的id。所以,如果我从available选择中选择三个,它只返回最后一个的id,如下所示:

{
   "_token": "KOqUu3m0uxwNxPC1BVisFT2y6P86z3uoBqZiEgTa",
   "available": "5",
   "enrol": ""
}

如何检索所有三个输入?

以下是来自源代码的实际HTML:

<form role="form" method="POST" action="/account/enrollments/1/update" class="form-horizontal" autocomplete="off">
    <input type="hidden" name="_token" value="KOqUu3m0uxwNxPC1BVisFT2y6P86z3uoBqZiEgTa">
    <div class="row">
        <div class="col-xs-12 col-md-4">
            <h4>Available Students</h4>
            <select multiple class="form-control" name="available">
                <option value="2">Student 2</option>
                <option value="4">Student 4</option>
                <option value="5">Student 5</option>
            </select>
        </div>

        <div class="col-xs-12 col-md-4 push-top">
            <button name="enrol" class="btn btn-block btn-effect-ripple btn-primary">Enrol Student(s)</button>
            <button name="unenrol" class="btn btn-block btn-effect-ripple btn-warning">Remove Student(s)</button>
        </div>

        <div class="col-xs-12 col-md-4">
            <h4>Currently Enrolled Students</h4>
            <select multiple class="form-control" name="enrolled">
                <option value="3">Student 3</option>
            </select>
        </div>
    </div>    
</form>

1 个答案:

答案 0 :(得分:6)

您似乎必须将[]放入name值:

name="available[]"

然后它返回一个数组。