我正在尝试删除html表中的字段。当我点击删除按钮时,我将用户重定向到一个弹出窗口,询问他们是否确定是否要删除表中的那一行。一旦他们单击是,它将把他们带到delete.php文件,该文件将其从我们的数据库中删除。我不知道如何将此值发送到我们的delete.php文件。
<!-- /.row -->
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>User Name</th>
<th>Distinct Items</th>
<th>Total Occurrences</th>
<th>Last Occurrence</th>
<th>Environment</th>
<th>Control</th>
</tr>
</thead>
<tbody>
<?php
include ("config.php");
$sql = "SELECT * FROM newTable";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['account_id'] . '</td>';
echo '<td>'. $row['user'] . '</td>';
echo '<td>'. $row['pass'] . '</td>';
echo '<td>'. $row['privs'] . '</td>';
echo '<td>'. $row['privValue'] . '</td>';
echo '<td>';
echo '<a href="#Modal_modify" class="btn btn-primary btn-xs" data-toggle="modal"><i class="fa fa-pencil"></i></a>';
echo '<a href="#Modal_delete" class="btn btn-danger btn-xs" data-toggle="modal"><i class="fa fa-trash-o "></i></a>';
echo '</td>';
echo '</tr>';
}
?>
<!-- Modal_delete HTML -->
<div id="Modal_delete" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Delete User</h4>
</div>
<div class="modal-body">
<p>Are you sure you want to delete this user?</p>
</div>
<form action="delete_user.php" method="post">
<input type="hidden" name="id" value="<?php echo $id;?>"/>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</form>
</div>
</div>
</div>
问题是创建删除按钮我引用了一个html子例程来创建一次点击弹出窗口。如何将表数据发送到我的delete.php文件?
<?php
include ("test.php");
$tbl_name="newTable"; // Table name
$id = $_POST['id'];
echo $_POST['id'];
echo "$id";
// To protect MySQL injection (more detail about MySQL injection)
$sql="DELETE FROM tbl_name WHERE account_id='$id';";
mysql_query(sql);
header("Location: adminUser.php");
?>
答案 0 :(得分:0)
您是否反对使用jquery? (对不起,我会事先在评论中问,但是,tsk,声誉不够,所以这是我的全部答案:)以下内容非常简单:
$('.btn-danger').click(function(event) {
$('#Modal_delete').css('display', 'initial'); // swap this for however you're showing your delete confirmation popup. I just had it for testing.
var rowToDeleteID = $(event.currentTarget).parents('tr').attr('id'); // you'd need to add a unique id to each table row for this to work.
$('input[type="hidden"]').val(rowToDeleteID);
});
当然,这会更加健壮(事实上,根据您的其他代码,它可能仅工作[更不用说它只是一个好主意])如果您添加唯一标识符删除按钮(带有fa-trash-o图标的标签)和隐藏的输入字段。
答案 1 :(得分:0)
您可以在$row['account_id']
链接中使用#Modal_delete
作为属性,并使用jQuery获取它。
<a href="#Modal_delete" class="btn btn-danger btn-xs" data-toggle="modal" data-account-id="<?= $row['account_id'] ?>"><i class="fa fa-trash-o "></i></a>
$("a[href='#Modal_delete']").on("click", function(e) {
$("input[name='id']").val($(this).data("account-id"));
});