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

Patwan's avatar

Dynamic fields in a table not working after hitting submit button

Am working on a Laravel app whereby am fetching some data from the controller and displaying on the view in a table structure (inside the tbody). On each row I have added a button and some hidden inputs that are enclosed in a form. When a user clicks on the button, the details of that particular row should be submitted.

The problem is when the button of a single row is clicked nothing happens.

 <table class="table table-hover mg-b-0 tx-11" id="policyTable">
                                       
<thead>
    <tr>
    <th>NAME</th>
    <th>INVOICE DATE</th>
    <th>VOUCHER NO.</th>
    <th>INVOICE AMOUNT</th>
    <th>KRA PIN</th>
    <th>STATUS</th>
    <th>PAYOUT</th>
    </tr>
</thead>
<tbody>
    @foreach($medical as $lifeC)
    <form method="POST" action="{{ route('b2b.medPayout') }}" id="payoutRequest">
        <!-- Hidden input fields on the form-->
        <input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">  
        <tr>
             <input type="hidden" name="agentName" id="agentName" value="{{ $lifeC->agent_name }}">
            <input type="hidden" name="voucherNo" id="voucherNo" value="{{ $lifeC->voucher_number }}">
            <input type="hidden" name="agentCode" id="agentCode" value="{{ $lifeC->agent_code }}">
            <input type="hidden" name="kraPin" id="kraPin" value="{{ $lifeC->kra_pin }}">

            <td class="add-color-dark">{{ $lifeC->agent_name }}</td>
            <td>{{ date( 'M j, Y', strtotime($lifeC->invoice_date)) }}</td>
            <td>{{ $lifeC->voucher_number }}</td>
            <td>{{ $lifeC->invoice_amount }}</td>
            <td>{{ $lifeC->kra_pin }}</td>
            <td>
                <span class="badge badge-danger">{{ $lifeC->payment_status }}</span>
            </td>
            <td>
                <button type="submit" class="btn btn-primary btn-sm" id="btnFront" style="cursor: pointer;"> Request </button>
            </td>
        </tr>
   <!--  </table> -->
    </form>     
    @endforeach
</tbody>
</table>
0 likes
2 replies
lostdreamer_nl's avatar
Level 53

It's probably because you have a bunch of invalid HTML, and your browser is going into quirksmode to make up for it.

A table can only have THEAD / TBODY / TR tags in it.

A TR tag can only have TD or TH in it.

Anything else will be placed somewhere else by your browser while loading the DOM, and as such the button is now probably outside of your form.

Try this:

    @foreach($medical as $lifeC)
        <tr>
            <td class="add-color-dark">{{ $lifeC->agent_name }}</td>
            <td>{{ date( 'M j, Y', strtotime($lifeC->invoice_date)) }}</td>
            <td>{{ $lifeC->voucher_number }}</td>
            <td>{{ $lifeC->invoice_amount }}</td>
            <td>{{ $lifeC->kra_pin }}</td>
            <td>
                <span class="badge badge-danger">{{ $lifeC->payment_status }}</span>
            </td>
            <td>
    <form method="POST" action="{{ route('b2b.medPayout') }}" id="payoutRequest">
        <!-- Hidden input fields on the form-->
        <input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">  
             <input type="hidden" name="agentName" id="agentName" value="{{ $lifeC->agent_name }}">
            <input type="hidden" name="voucherNo" id="voucherNo" value="{{ $lifeC->voucher_number }}">
            <input type="hidden" name="agentCode" id="agentCode" value="{{ $lifeC->agent_code }}">
            <input type="hidden" name="kraPin" id="kraPin" value="{{ $lifeC->kra_pin }}">

                <button type="submit" class="btn btn-primary btn-sm" id="btnFront" style="cursor: pointer;"> Request </button>
    </form>     
            </td>
        </tr>
    @endforeach

Now the whole form, hidden inputs, and submit button will be inside the last TD, which is valid HTML.

Please or to participate in this conversation.