![](/img/trans.png)
[英]How to delete a row in a table in Angular4 . The delete button is in each row
[英]Delete button for each row in a table PHP
<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)檢索它。
這兩個腳本可以。
show.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");
}
?>
delete.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>
?>
您也可以從輸入中獲取ID(假設您正在使用jquery)
var id = $(e).parent().find('input[type="hidden"]').val();
未測試
在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");
});
});
在delete.php上:
<?php
if(isset($_POST['userID'])){
$id = $_POST['userID'];
include('connect-db.php');
mysql_query("DELETE FROM table_name WHERE userID = " . $id);
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.