简体   繁体   中英

How to get 'id' from table to modal to delete it with 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM