表 PHP 中每一行的删除按钮

Delete button for each row in a table PHP

本文关键字:一行 删除 按钮 PHP      更新时间:2023-09-26
<td style="text-align: left;">
    <?php
          echo '<button type="button" class="btn btn-danger delete delete-account"><i class="fa fa-trash-o fa-lg"></i> Delete</a></button> 
                <input type="hidden" value="'. $r['user_id'] .'" name="delete[]">';
    ?>
</td>

我有这段带有此 javascript 的代码:

$('button.delete-account').click(function(e) {
    swal({
        title: "Are you sure?",
        text: " <?php echo $r['user_id'] ?> You will not be able to undo this acction !",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, delete it!",
        closeOnConfirm: false,
        html: false
    }, function() {
        swal("Deleted!", "Your imaginary file has been deleted.", "success");
    });
});

$r['user_id']--->这基本上是一个帮助我从数据库中提取所有用户的函数,在本例中,它将拉取所有user_id。

问题是我在每行都有不同的用户,当我单击删除按钮时,我得到的是相同的user_id而不是个人 id。

我如何能够单独调用每个用户???

谢谢大家:)

更新:无法完全理解您的代码,但您可以通过查看此小提琴来了解我的想法: 示例

使用创建的按钮的值设置用户的 id 是一种方法。这样,您将为每个创建的按钮分配一个值,该值表示用户的 id。

<td style="text-align: left;">
    <?php
          echo '<button type="button" value="'. $r['user_id'] .'" class="btn btn-danger delete delete-account"><i class="fa fa-trash-o fa-lg"></i> Delete</a></button>';
    ?>
</td>
然后在您的jquery中,通过使用

jquery的$(this)获取单击的按钮的值,并通过调用.val()获取值。这样,您所有创建的按钮只有一个处理程序。:

$('button.delete-account').click(function(e) {
swal({
title: "Are you sure?",
text: $(this).val()+" You will not be able to undo this acction !",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false,
html: false
}, function(){
swal("Deleted!",
"Your imaginary file has been deleted.",
"success");
});
});

代码未测试...我不太能够验证您的代码是如何工作的,所以我只是复制了您的代码并使用按钮的值进行了微小的更改..但最重要的是,你要了解在每个按钮上存储用户的值(id),并在处理中使用$(this)检索它。

你也可以

从输入中获取id(我假设你正在使用jquery)

var id = $(e).parent().find('input[type="hidden"]').val();

未测试

这两个脚本就可以了。

显示.php

<?php
/* 
Deletes a specific entry from the  table
*/
// connect to the database
include('connect-db.php');
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
// get id value
$id = $_GET['id'];
// delete the entry
$result = mysql_query("DELETE FROM *fillin* WHERE id=$id")
or die(mysql_error()); 
// redirect back to the view page
 header("Location: urlocation.php");
 }
else
 // if id isn't set, or isn't valid, redirect back to view page
 {
header("Location: urlocation.php");
}
?>

删除.php

    <?php
    */  Displays all data from ' table

    // connect to the database
    include('dbconnectfile.php');
    // get results from database
    $result = mysql_query("SELECT * FROM dbase") 
            or die(mysql_error());  
      // display data in table
  echo "<p><b>info</b></p>";
    echo "<table border='2' border-color='#800000' cellpadding='10'>";
    echo "<tr> <th>fill-in</th> <th>fill-in</th><th>fill-in</th><th>Delete</th></tr>";
    // loop through results of database query, displaying them in the table
    while($row = mysql_fetch_array( $result )) {
            // echo out the contents of each row into a table
            echo "<tr>";
            echo '<td>' . $row['rowname'] . '</td>';
            echo '<td>' . $row['rowname'] . '</td>';
            echo '<td>' . $row['rowname'] . '</td>';
            echo '<td><a HREF="delete.php?id=' . $row['id'] . '"><img style="width: 25px; height: 25px; " alt="" src="">Delete</a></td>';
            echo "</tr>"; 
    } 
    // close table>
    ?>

在 html 上:

<td style="text-align: left;">
        <?php
              echo '<button type="button" class="btn btn-danger delete delete-account"><i class="fa fa-trash-o fa-lg"></i> Delete</a></button> 
                    <input type="hidden" class="userId" value="'. $r['user_id'] .'" name="delete[]">';
        ?>
    </td>

在 js 上:

$('button.delete-account').click(function(e) {
   var id = $(this).siblings('input.userId').val();
  swal({
   title: "Are you sure?",
   text: id + " You will not be able to undo this acction !",
   type: "warning",
   showCancelButton: true,
   confirmButtonColor: "#DD6B55",
   confirmButtonText: "Yes, delete it!",
   closeOnConfirm: false,
   html: false
  }, function(){ 
   $.post( "delete.php", { userID: id } );
   swal("Deleted!",
     "Your imaginary file has been deleted.",
     "success");  
  });
});

删除时.php:

<?php
if(isset($_POST['userID'])){
  $id = $_POST['userID'];
  include('connect-db.php');
  mysql_query("DELETE FROM table_name WHERE userID = " . $id);
}
?>