我在数据表中有一个删除按钮,如下所示:
<div class="card mb-3" style="width:70%;">
<div class="card-header">
<i class="fas fa-table"></i>
Perusahaan</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Kode Perusahaan</th>
<th>Nama Perusahaan</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$conn = mysqli_connect("127.0.0.1","root","","penggajian");
$sql = "SELECT * FROM perusahaan";
$getperusahaan = mysqli_query($conn,$sql);
while($list = mysqli_fetch_array($getperusahaan, MYSQLI_ASSOC)){
echo "<tr><td>";
echo $list['kode'];
echo "</td><td>";
echo $list['nama'];
echo "</td><td>";
?>
<button type="button" data-id="<?php echo $list['id_perusahaan']; ?>" class="btn btn-primary btn-sm passingID" data-toggle="modal" data-target="#editperusahaan"><i class="fas fa-pencil-alt"></i> Edit</button></a>
<button type="button" data-toggle="modal" data-target="#konfirmasi" class="btn btn-danger btn-sm delete" style="margin-left: 10px;"><i class="far fa-times-circle"></i> Delete</button>
</td></tr>
<?php
}
?>
</tbody>
</table>
</div>
当我单击删除按钮时,我想在引导模态上进行确认,然后用户才能删除它。这是我的模态:
<div class="modal fade" id="konfirmasi" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Konfirmasi Penghapusan Data</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Hapus Data?</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<a href="hapusperusahaan.php?id=<?php echo $list['id_perusahaan'];?>" class="btn btn-danger" href="logout.php">Hapus</a>
</div>
</div>
</div>
</div>
问题是我不知道如何将$list['id']
从数据表传递到我的模式
答案 0 :(得分:3)
将其替换为表格列表
<button data-id="<?php echo $list['id'];?>" type="button" data-toggle="modal" data-target="#konfirmasi" class="btn btn-danger btn-sm delete-button" style="margin-left: 10px;"><i class="far fa-times-circle"></i> Delete</button>
将其替换为模式弹出HTML
<a data-id="" class="btn btn-danger confirm-delete">Hapus</a>
添加JavaScript代码
$('.delete-button').on('click', function (e) {
var id = $(this).attr('data-id');
$('.confirm-delete').attr('data-id',id);
});
$(".confirm-delete").on('click', function (e) {
var id = $(this).attr('data-id');
console.log(id);
location.href="hapusperusahaan.php?id="+id;
});
答案 1 :(得分:1)
尝试一下:
button class="btn btn-danger btn-sm remove">Delete</button>
// delete.php
$id = $_GET['id'];
//Connect DB
$conn = mysqli_connect("127.0.0.1","root","","penggajian");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM perusahaan WHERE id = $id";
echo"sucess message";
// js
$(".remove").click(function(){
var id = $(this).parents("tr").attr("id");
if(confirm('Are you sure to delete this record ?'))
{
$.ajax({
url: '/delete.php',
type: 'GET',
data: {id: id},
error: function() {
alert('Something is wrong');
},
success: function(data) {
$("#"+id).remove();
alert("Record deleted successfully");
}
});
}
});