babbaggeii
babbaggeii

Reputation: 7737

Only retrieving last option in multiple select element

The form is posted and I'm retrieving the inputs with:

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

The form is like this:

<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>

But the response only shows the id of the last one selected. So, if I select three from the available select, it only returns the id of the last one, like this:

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

How do I retrieve all three inputs?

Here's the actual HTML from source:

<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>

Upvotes: 1

Views: 1067

Answers (1)

babbaggeii
babbaggeii

Reputation: 7737

It seems that you have to put[] in the name value:

name="available[]"

Then it returns an array.

Upvotes: 7

Related Questions