单击img后,如何解决当前行的删除问题?

时间:2019-03-26 12:00:23

标签: javascript php modal-dialog

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

在较早的搜索中,我尝试并未能实施a different question中的提示。

    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>

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

2 个答案:

答案 0 :(得分:0)

只需在该输入上创建一个链接,它就可以正常工作。

<a href='delete-user/delete-user.php?username=<?=$row['username']?>'>
<input class="modal-button" type="button" value="I'm sure" />
</a>

或者,您可以这样做

onclick="deleteUser('<?=$row['username']?>')"

function deleteUser(username){
    window.location.href = "project_url_delete"+username;
}

答案 1 :(得分:0)

    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');
 }

尝试一下...