php - 将表格数据发送到弹出删除

标签 php html mysql css

我正在尝试删除我的 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">&times;</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");




?>

最佳答案

你反对使用 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 图标的 a 标签)和隐藏的输入字段。

关于php - 将表格数据发送到弹出删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25588280/

相关文章:

php - 将 IP 地址与 IPv6 block 进行比较

php - 在 laravel 中禁用特定路由的 csrf

php - Linux 服务器 : Send mail to my users

html - 网页上正确的标题结构?

MySQL 根据给定的迟到百分比计算准点率

服务器更改了 php 代码顺序?

html - 如何阻止 CSS nowrap 在我呈现的文档中放置换行符?

html 并将 span ID 合并为一个 span ID

mysql - 如何在 MySQL 查询中使用 DISTINCT 而不在输出中显示该字段?

php - 插入 SQL 时保留 JSON 数据的顺序