<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'] ---&GT;这基本上是一个帮助我从我的数据库中拉出所有用户的函数,对于这个例子,它将拉取所有user_id。
问题是我在每一行都有不同的用户,当我点击删除按钮时,我得到的是同一个user_id,而不是个人ID。
我如何能够单独呼叫每个用户???
谢谢你们:)
答案 0 :(得分:2)
更新:无法完全理解您的代码,但您可以通过查看这个小提琴来了解我的想法:Example
使用创建按钮的值设置用户的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)检索它。
答案 1 :(得分:1)
这两个脚本都可以。
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>
?>
答案 2 :(得分:1)
您也可以从输入中获取id(我假设您使用的是jquery)
var id = $(e).parent().find('input[type="hidden"]').val();
未经过测试
答案 3 :(得分:1)
on 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>
on 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");
});
});
on delete.php:
<?php
if(isset($_POST['userID'])){
$id = $_POST['userID'];
include('connect-db.php');
mysql_query("DELETE FROM table_name WHERE userID = " . $id);
}
?>