Reputation: 4480
I created a form and filled out most of the fields with data from my db using a foreach loop for fill out several rows. The problem I am having is how to submit just one row of information to my controller. As of now the form always submits the last row of data. So if I submit the second row the data that gets sent to my controller is the information from the last row. What can I do so when I click on the button for the second row, I get sent the info for my second row to my controller.
Here is my form code
<form action="/jadmin/updatebulkexpirationdate" method="post">
@foreach ($users as $user)
<tr>
<td id="name" value="{{$user->domain}}">{{ $user->domain}}</td>
<input type="hidden" name="domain" value="{{$user->domain}}">
<td>{{ $user->where('plan_expiration', $user->plan_expiration)->where('domain', $user->domain)->count()}}</td>
<td id="oldDate>{{ $user->plan_expiration }}</td>
<input type="hidden" name="oldDate" value="{{$user->plan_expiration}}">
<td><button class="btn btn-default" id="newBulkExpiratioDateUpdate" type="submit">Set New Expiration Date</button></td>
</tr>
@endforeach
</form>
Upvotes: 1
Views: 3439
Reputation: 256
Each time you create these inputs in the foreach loop, you're overwriting which input is sent with the request. However, if you put [] after your name, it will be interpreted as an array and you'll be able to access all the data. For example:
<input type="hidden" name="domains[]" value="{{$user->domain}}">
And so on for the other ones. Run a dd($request) after you submit the form and you'll see that the request will have all the domains in an array, not just the last one.
EDIT: I realize I didn't actually address the main question you asked, but just gave you a fix for something related. I'd recommend putting the whole form in the foreach and then having each button post to a route which has the id of the user you want to alter. If you'd like some help with how to get that done, I can go into more detail.
Upvotes: 1