dt4320279
2018-10-12 19:35 阅读 30
已采纳

如何从表到模态获取“id”以使用php删除它

Hi I'm a super begginer in web developing. I need to take my table ID to a modal in order to delete the mySQL record. This is my code:

<?php while($llenartabla = $getsibella->fetch_assoc()) { ?>
<tr>
    <th scope="row"><?=$llenartabla['id']?></th>
    <td><?=$llenartabla['date']?></td>
    <td><?=$llenartabla['location']?></td>
    <td><?=$llenartabla['city']?></td>
    <td><button type="button" class="btn btn-danger" data-toggle="modal" data-target="#myModal">Delete</button></td>
</tr>
<?php }?>

And here is my code for the modal:

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Delete</h4>
            </div>
            <div class="modal-body">
                Are you sure you want to delete the event?
            </div>
            <div class="modal-footer">
                <a href="delete.php?id='I NEED THE ID HERE'" class="btn btn-danger">Delete</a>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

I know it's kind of an easy question, but I'm learning bootstrap and php haha. So any answers or comments are welcome :p

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douao3063 douao3063 2018-10-12 20:23

    Here's a newbie solution for you:

    <?php while($llenartabla = $getsibella->fetch_assoc()) { ?>
    <tr>
        <th scope="row"><?=$llenartabla['id']?></th>
        <td><?=$llenartabla['date']?></td>
        <td><?=$llenartabla['location']?></td>
        <td><?=$llenartabla['city']?></td>
        <td><button type="button" class="btn btn-danger" data-toggle="modal" data-target="#myModal<?=$llenartabla['id']?>">Delete</button></td>
    </tr>
    <?php }?>
    

    just changed the following part: data-target="#myModal<?=$llenartabla['id']?>"

    As for modal: again wrapped around the same loop with changed id of modal id="myModal<?=$llenartabla['id']?>" and your delete url modified delete.php?id=$llenartabla['id']

    <?php while($llenartabla = $getsibella->fetch_assoc()) { ?>
    <div class="modal fade" id="myModal<?=$llenartabla['id']?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Delete</h4>
                </div>
                <div class="modal-body">
                    Are you sure you want to delete the event?
                </div>
                <div class="modal-footer">
                    <a href="delete.php?id=$llenartabla['id']" class="btn btn-danger">Delete</a>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
    <?php }?>
    

    Now if you want the advance solution you need javascript.

    点赞 评论 复制链接分享

相关推荐