Delete Confirm Bootstrap Modal Passing ID

Hi All

I am struggling to find an answer to my question… maybe I am searching the wrong keywords?

I am writing an internal site for myself in PHP, MySQL etc and have a delete option all over the place using an icon and a Bootstrap 4 Modal for the confirm before deleting.

As a work around I have put the Modal in the while loop and included the uniqueID in each modal… this works fine now however, in the future, there could be thousands of entries which would mean the same code is created thousands of times… which I do not think is necessary.

What I think I need to do is have the delete confirm modal added once and on the icon have the uniqueID and dynamically pass the uniqueID to the modal however I cannot work out how to do that.

It would be great if you could tell me how or point me in the right direction.

Thanks for any help.


In that case you’d indeed create just one modal, and then add a data attribute to all buttons indicating the ID it’s for (i.e. <button ... data-id="123">) and then when the button is clicked:

  1. Copy the data-id from the button to a hidden input in the modal (assuming the modal uses a <form>)
  2. Show the modal