Delete Button In A Loop Giving Same ID In Laravel App

- 1 answer

Good day. There is a list of items with two different buttons in my laravel app. One is for updating while the other deletes. I'm using a form (with POST methods) for the two buttons. So, basically, I have two forms on the page. The "update" button works quite well. But the delete button keep on giving me the same ID when I "dd" the request. I am using javascript to submit the "Delete" form because I want to use sweet alert for the user to confirm delete. The code is shown below. Please what may be wrong with it. Also, I'll be glad if I can get a better way of doing it.

This are my forms

                            <form action="{{route('update.cart')}}" method="POST">
                                <input type="hidden" name="id" value="{{$product->id}}">
                            <td class="product-quantity"><input type="number" name="quantity" value="{{$product->quantity}}"></td>

                                <td class="product-subtotal">₦{{$product->price * $product->quantity}}</td>
                            <td class="product-remove">
                                <input type="submit" class="login-btn" value="Update"  style="width:90px!important;text-transform: none!important;">


                            <form style="display: inline!important;" action="{{route('remove.cart')}}" method="POST" name="myForm" id="myForm">
                                $id = $product->id;
                              <input type="hidden" name="cart_id" value="<?php echo $id?>">
                            <button target="_blank" rel="nofollow noreferrer" href="#" id="remove" type="button" onclick="removeItem({{$product->id}})"><i class="fa fa-trash" aria-hidden="true"></i></button>



My Javascript Code To Submit The Form

    function removeItem(clicked){


Controller This gives me the same id in my controller irrespective of the button clicked

        $item = $request->cart_id;




You are using the id myForm for every delete form. A page is only allowed to have an id once, because you have it multiple times, it always submits the first one.

Either remove the javascript and let the form submit directly or add unique id's for every form.