【发布时间】:2016-06-10 12:53:36
【问题描述】:
我从数据库中检索数据并填充到表中。对于每一行,都有一个复选框,用户可以在其中选择多行并将其删除。我怎样才能做到这一点,当用户在选中表格上的复选框后单击删除按钮时,将出现一个引导模式作为删除这些选定行的确认。然后,用户将单击引导模式上的确认按钮以从数据库中删除记录。目前,当我单击引导模式上的确认按钮时,它不会删除数据库上的记录。
index.php (PHP)
<?php
include_once 'configuration.php';
session_start();
$message = "";
if((isset($_SESSION['username']) == ""))
{
header("Location: index.php");
}
$sql = "SELECT id, title FROM books";
$query = mysqli_query($db, $sql);
?>
index.php (HTML)
<div class="content">
<form id="booksForm" action="" method="POST">
<table id="booksTable" class="col-md-12 table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th><input type="checkbox" class="chkAll" value="" /></th>
<th>#</th>
<th>Title</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
while($result = mysqli_fetch_array($query))
{
$html = '<tr>
<td data-title="">
<input type="checkbox" name="chkDelete[]" value="' . $result['id'] . '">
</td>
<td data-title="#">' . $result['id'] . '</td>
<td data-title="Title">' . $result['title'] . '</td>
<td data-title="Action">
<a href="edit.php?id=' . $result['id'] . '">Edit</a>
</td>
</tr>';
echo $html;
}
?>
</tbody>
</table>
<input type="button" id="btnDelete" value="Delete" data-toggle="modal" data-target="#confirmDelete"/>
<div class="message"><?php echo $message;?></div>
</form>
<div id="confirmDelete" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
Delete selected data?
</div>
<div class="modal-body">
Click confirm to delete data permanently.
</div>
<div class="modal-footer">
<button class="btn btn-success" id="btnConfirm">Confirm</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div> <!--//END modal-footer-->
</div> <!--//END modal-content-->
</div> <!--//END modal-dialog-->
</div> <!--//END confirmDelete-->
</div> <!--//END content-->
<script type="text/javascript" src="resource/js/bootstrap.min.js"></script>
<script>
$('#btnConfirm').click(function() {
$.ajax({
type: "POST",
url: "deleteBooks.php",
data: $('confirmDelete').serialize(),
success: function(msg){
alert("success");
$("#confirmDelete").modal('hide');
},
error: function(){
alert("failure");
}
});
});
</script>
删除Books.php (PHP)
<?php
include_once 'configuration.php';
if(isset($_POST['btnConfirm']))
{
$checkbox = isset($_POST['chkDelete']) ? $_POST['chkDelete'] : array();
$id = 0;
for($i=0;$i<count($checkbox);$i++)
{
$id = $checkbox[$i];
$deleteQuery = "DELETE FROM books WHERE id='$id'";
$DeleteQueryExec = mysqli_query($db, $deleteQuery);
}
}
?>
【问题讨论】:
-
你是否包含了 include_once 'configuration.php';在您的 deleteBooks.php 页面中?
-
@Plum 是的,包括在内。仍然没有删除行。
标签: php jquery twitter-bootstrap checkbox bootstrap-modal