Skip to content
Advertisement

When submitting this button inside a datatable doesnt submit the right row id

I have a dynamic table which is set inside a foreach, so for each item of the array fetched create a new row. I have in the last column a button for each row. When clicking that submit button I am suppose to receive the id of that in PHP. Submission is being done correctly, but I am receiving the wrong id in PHP. Its basically taking the last id of the array when submitting. Any idea why?

Here is the table:

<form method="post" id="frm-example" action="<?php echo $_SERVER["PHP_SELF"] . '?' . e(http_build_query($_GET)); ?>">
            <table id="example" class="display compact">
            <thead>
                <th>Device</th>                    
                <th>Sales date</th>
                <th>Client comments</th>  
                <th>Breakage count</th>
            </thead>
            <tbody>
                <?php foreach ($arr_cases_devices as $cases) {  ?>
                    <tr>
                        <td>
                            <?php echo $cases['name']; ?>
                        </td>
                        <td>
                            <?php echo $cases["sales_date"]; ?>
                        </td>
                        <td>
                            <?php echo $cases["dev_comment"]; ?>
                        </td>
                        <td>          
                           <input type="hidden" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>" />
                           <button type="submit" name="see_rma">See RMA</button>                       
                        </td>
                    </tr>
                <?php } ?>
            </tbody>
        </table>
   </form>

When clicking on see_rma this is what I receive in PHP:

if (isset($_POST['see_rma'])) {
   $selected_dev = e($_POST['device_id_breakage']);
   print_r($selected_dev); // prints the "Dev_Id" of the last row, not of the row clicked
}

If I try printing $cases["Dev_Id"]; inside loop in the table, it prints perfectly fine, so it prints the Dev_Id of each row correctly. So, that means there is nothing wrong with the array or data. I don’t why is this happening but it’s for sure the first time I am having this issue.

I do this in many other tables but for some reasons in this one its not working properly.

Advertisement

Answer

You have multiple <input> elements with the same name within your form, and all of them are going to be submitted when you submit the form, but PHP can only get one of them. That’s why you end up with only the last one in $_POST.

It looks like you should be able to fix this by just moving some attributes from the hidden input into the button (replacing the hidden input).

<button type="submit" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>">
    See RMA
</button>

Only the button that was clicked will be submitted. Note that after changing the name of the button, you won’t have see_rma in $_POST any more, so if you have any code that depends on that you’ll need to change it to look for the other name instead.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement