如何在删除前添加确认消息?

How to add confirmation message before deleting?

我想在删除 table 中的数据之前添加一条确认消息。但我不知道如何以及在哪里放置代码。我尝试查找其他代码,但它不起作用。
这是我的代码:

        <?php $a = 0?>

        <?php while ($row = mysqli_fetch_array($res)) { 

            echo "<tr id=".$a++.">
                <th scope='row' class='row-data'>".$row['user_id']."</th>
                <td class='row-data'>".$row['full_name']."</td>
                <td class='row-data'>".$row['user_name']."</td>
                <td><input type='button' 
                    value='EDIT'
                           onclick='editUser()'' /></td>
                <td><input type='button' 
                    value='DELETE' 
                      onclick='deleteUser()' /></td>
            </tr>
        
        </tbody>";
       }; ?>
 

        function deleteUser() {
            var rowId = 
                event.target.parentNode.parentNode.id;
          //this gives id of tr whose button was clicked
            var data = 
            document.getElementById(rowId).querySelectorAll(".row-data"); 
          /*returns array of all elements with 
          "row-data" class within the row with given id*/

            var uID = data[0].innerHTML;

            document.getElementById("toBeEdit").value = uID;
            document.getElementById("taskStatus").value = 'delete';
            //alert("ID: " + uID);
            
            const form  = document.getElementById('editForm');

            form.submit();

        }

    </script>

您可以在 deleteUser() 函数中使用 built-in 方法 confirm()。请看下面的代码,

function deleteUser() {
         if(confirm('Are you sure want to delete user?')){
            var rowId = 
                event.target.parentNode.parentNode.id;
          //this gives id of tr whose button was clicked
            var data = 
            document.getElementById(rowId).querySelectorAll(".row-data"); 
          /*returns array of all elements with 
          "row-data" class within the row with given id*/

            var uID = data[0].innerHTML;

            document.getElementById("toBeEdit").value = uID;
            document.getElementById("taskStatus").value = 'delete';
            //alert("ID: " + uID);
            
            const form  = document.getElementById('editForm');

            form.submit();
          }

        }

confirm() 方法显示一个对话框,其中包含一条消息、一个“确定”按钮和一个“取消”按钮。

confirm() 方法 returns 如果用户单击“确定”则为真,否则为假。

您应该添加此代码包括您的删除功能

if(window.confirm("do you want delete this data")){
    -->your code with js<--
}else{
    return false;
}