首页 > 解决方案 > 单击img时如何修复当前行的删除?

问题描述

我一直在努力尝试使用模式作为确认从我的数据库中删除特定行。SQL 有效,但仅在没有模式的情况下有效。(模态是必需的)

从较早的搜索中,我尝试并未能实施来自不同问题的提示。

    while ($row = $statement ->fetch(PDO::FETCH_ASSOC)) {

    Here I create a variable to use later
    <?php $username = $row['username']; ?>   

        <td>
            <div class="adjust-buttons">

                <input 
                type="image" 
                id="delete" 
                alt="delete" 
                src="../img/delete.png" 
                onclick="showModal();" 
                onmouseover="this.src='../img/delete-hover.png';" 
                onmouseout="this.src='../img/delete.png';">


                <div id="myModal" class="modal">
                    <div class="modal-content">

                        <h3>Delete user</h3>

                        <?php
                    echo 'are you sure you want ' .$username. '     removed?';
                        ?>

                        <input 
                            class="modal-button" 
                            type="button" 
                            value="Cancel" 
                            onclick="hideModal();">

                        <input 
                            class="modal-button" 
                            type="button" 
                            value="I'm sure" 
                            id="<?php echo $row['username']; ?>" 
                            onclick="document.location.href='../delete-user/delete-user.php?username=<?php echo $row['username']?>'" />
                        </div>

                    </div>
                </div>
        </td>
    </tr>

    <?php } ?>
</table>

我希望删除的行是从循环中选择的行。情况并非如此,无论我按下哪个删除按钮,都只会选择第一个创建的行。

标签: javascriptphpmodal-dialog

解决方案


    while ($row = $statement ->fetch(PDO::FETCH_ASSOC)) {

Here I create a variable to use later
<?php $username = $row['username']; ?>   

    <td>
        <div class="adjust-buttons">

            <input 
            type="image" 
            id="delete" 
            alt="delete" 
            src="../img/delete.png" 
            onclick="showModal('<?php echo $row['username']; ?>');" 
            onmouseover="this.src='../img/delete-hover.png';" 
            onmouseout="this.src='../img/delete.png';">


            <div id="myModal<?php echo $row['username']; ?>" class="modal">
                <div class="modal-content">

                    <h3>Delete user</h3>

                    <?php
                echo 'are you sure you want ' .$username. '     removed?';
                    ?>

                    <input 
                        class="modal-button" 
                        type="button" 
                        value="Cancel" 
                        onclick="hideModal();">

                    <input 
                        class="modal-button" 
                        type="button" 
                        value="I'm sure" 
                        id="<?php echo $row['username']; ?>" 
                        onclick="document.location.href='../delete-user/delete-user.php?username=<?php echo $row['username']?>'" />
                    </div>

                </div>
            </div>
    </td>
</tr>

<?php } ?>

和onclick方法

function showModal(modal_id)
{
  $('#myModal'+modal_id).show('modal');
 }

尝试这个...


推荐阅读