Alex Winter
Alex Winter

Reputation: 201

Make a form be dynamic?

I have a foreach loop which creates multiple textboxes and I have one submit button. After submit is hit I would like all the data to be sent to the controller.

{{ Form::open(array('route' => 'shift.startdata_post', 'class' => 'form-horizontal')) }}    
    <tbody>
        <td> {{ $game->name }} (Serial Number: {{ $game->serial }})</td>
        <td> {{ Form::text('cash', null, array('autofocus' => 'autofocus', 'placeholder' => 'Amount in till')) }} </td>
        <td> {{ Form::text('unsold', null, array('placeholder' => 'Amount of unsold tickets')) }} </td>
    </tbody>    
@endif

Here is what it looks like:

Right now its only sending the very last two textboxes and ignoring the first two. How do I get it to send all the textboxes.

Does anyone have any ideas?

Upvotes: 2

Views: 83

Answers (1)

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

Input fields must have unique name values. Looks like you are using the same name values on each iteration.

For example:

<input type="text" name="yourName"><br>
<input type="text" name="yourAge"><br>
<br>
<input type="text" name="yourName"><br>
<input type="text" name="yourAge"><br>

In this example only the last two input fields will be used. When there is more than one input field, with the same name, the last one takes precedence.

In order to fix your form you must make the names unique. Appending a number would be one way, e.g.

<input type="text" name="yourName-1"><br>
<input type="text" name="yourAge-1"><br>
<br>
<input type="text" name="yourName-2"><br>
<input type="text" name="yourAge-2"><br>

Another way would be to use multidimensional arrays, like so:

<input type="text" name="person[1][name]"><br>
<input type="text" name="person[1][age]"><br>
<br>
<input type="text" name="person[2][name]"><br>
<input type="text" name="person[2][age]"><br>

Upvotes: 2

Related Questions