dt4320279 2018-10-12 19:35
浏览 33
已采纳

如何从表到模态获取“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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料