Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jhutto's avatar

Update database table from a table of form data

I'm trying to update a database table from a table with a list of names with an additional column with a checkbox. The user is checking the box to indicate that someone in the list attended the event.

I'm loading the view in a table format with names and a column for the checkbox.

The problem I'm having is how to identify the row of each column without displaying the id for each name in the database. If I use a hidden column for the id... it doesn't get transfered.

I am able to pass the id by adding the id to the value on the checkbox but then it comes of as an array.

Here's the form:

<form id="eventpracnames" action="/events/updateeventnames" method="POST">
                                        {{ Method_field ('Patch')}}
                                        {{csrf_field()}}

                                        <div class="row justify-content-left">
                                                        <header id="EventsEditnameheader">
                                                                <table class="table table-striped " >
                                                                <thead>
                                                                        <tr>
                                                                                <th style="width: 25%;">First Name</th>
                                                                                <th style="width: 25%;">LastName</th>
                                                                                <th style="width: 29%;">Attended</th>
                                                                                
                                                                        </tr>
                                                                </thead>
                                                                </table>
                                                
                                                        </header>
                                                
                                        </div>
                        
                                        <div class="row justify-content-left">
                        
                                                <div class="tableEventsEditFixHead">
                                                <table class="table table-striped table-bordered pracindex">
                        
                                                                <tbody>
                                                                @foreach($requiredEventHistoryPracDetails as $key => $requiredEventHistoryPracDetail)
                                                                
                                                                <tr>
                                                                <td class="w-20" style="background: white;">{{$requiredEventHistoryPracDetail->FirstName}}</td>  
                                                                <td class="w-20" style="background: white;">{{$requiredEventHistoryPracDetail->LastName}}</td> 
                                                                <td class="w-20" style="text-align:center;"><input type="checkbox" name="pracid" id="{{$requiredEventHistoryPracDetail->id}}" @if($requiredEventHistoryPracDetail->Attended) checked @endif value="1"> </td>
                                                                </tr>
                                                                
                                                                @endforeach
                                                                </tbody>
                                                                </table>
                        
                                                </div>
                                        </div>

                                        <div class="form-row" style="width: 60%; margin-top:25px;">
                                              <input type="submit" class="btn btn-primary btn-lg" value="Save Changes">
                                        </div>
                                </form>

Any ideas would be appreciated. Thanks

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

If you are having multiple checkboxes with the same name attribute, then you should have an array input, e.g. name="pracid[]" if you set the value attribute to the id of the model, then whenever the form is submitted you will get an array of id's which should true.

<input 
    type="checkbox"
    name="pracid[]"
    @if($requiredEventHistoryPracDetail->Attended) checked @endif
    value="{{$requiredEventHistoryPracDetail->id}}"
>

By default, unchecked checkboxes are not submitted, so you would need to modify your approach if this is a requirement

jhutto's avatar

Perfect... That makes so much sense. Thanks for your help.

1 like

Please or to participate in this conversation.