[英]PHP bootstrap 4 modal delete button in each row
Good day! 美好的一天! I got a error that says "Undefined variable: id".
我收到一个错误消息:“未定义的变量:id”。
I created a table which has a delete button in each row, when clicked pops up an modal and asks if I want to delete the row. 我创建了一个表格,该表格的每一行都有一个删除按钮,单击后会弹出一个模态对话框,询问我是否要删除该行。 But when ever I click on the delete button it says the error Undefined variable: id and doesn't delete the data.
但是,每当我单击删除按钮时,都会显示错误未定义变量:id,并且不会删除数据。
Here's my table code: 这是我的表格代码:
if(isset($_POST['delete'])) {
$data->delete_multiple($id, $conn);
}
<table class="table table-hover table-bordered" id="table1">
<thead>
<tr>
<th>Faculty Code</th>
<th>Last Name</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Position</th>
<th></th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php while ($row = $result -> fetch_object()): ?>
<tr>
<td><?php echo $row->Faculty_ID;?></td>
<td><?php echo $row->Faculty_Lastname ?></td>
<td><?php echo $row->Faculty_Firstname ?></td>
<td><?php echo $row->Faculty_Middlename?></td>
<td><?php echo $row->Position; ?></td>
<td class="text-center">
<a class="btn btn-sm btn-outline-primary text-muted" href="Faculty_edit.php?Faculty_ID=<?php echo $row->Faculty_ID;?>">Edit</a>
<a type="button" title="Delete Faculty" data-toggle="modal" data-target="#delete_modal" class="btn btn-sm btn-outline-danger" >Delete</button></td>
<td>
<?php if($row->Status=='Active') echo '<a href="#deactive_account" data-toggle="modal" data-id='.$row->Faculty_ID.' class="activate" style="color:green;">Active</a>'; ?>
<?php if($row->Status=='Inactive') echo '<a href="#active_modal" data-toggle="modal" data-id='.$row->Faculty_ID.' class="activate" style="color:red;">Inactive</a>'; ?>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
My modal code: 我的模态代码:
<div class="modal fade" id="delete_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Delete Confirmaiton</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="alert alert-danger" role="alert">
Are you sure you want to delete selected record?
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-danger" name="delete">Yes</button>
</div>
</div>
</div>
</div>
Delete function code: 删除功能码:
function delete_multiple($id, $conn){
$id = $_POST['delete'];
$sql = "DELETE FROM faculty WHERE Faculty_ID=? ";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
}
That's because you are not passing the row id to the modal popup. 那是因为您没有将行ID传递给模式弹出窗口。
Add the faculty id to your delete button and add a unique class to it 'delete-faculty-btn'. 将教职员工ID添加到您的“删除”按钮,并为其添加一个唯一的班级“ delete-faculty-btn”。 Ref.
参考。 to below line
到下一行
<a type="button" data-fid="<?php echo $row->Faculty_ID;?>" title="Delete Faculty" data-toggle="modal" data-target="#delete_modal" class="btn btn-sm btn-outline-danger delete-faculty-btn" >Delete</button></td>
Change your modal popup footer to:- 将您的模式弹出式页脚更改为:-
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
<input type="hidden" name="delete" value="" id="row-id-to-delete" />
<button type="submit" class="btn btn-danger" >Yes</button>
</div>
And add the below Javascript Script:- 并添加以下Javascript脚本:-
$(document).on('click', '.delete-faculty-btn', function(){
$("#row-id-to-delete").val($(this).data('fid'));
});
My assumption is that you have the submit button in your modal popup inside a HTML form. 我的假设是在HTML表单内的模式弹出窗口中有“提交”按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.