onclick delete 将始终删除 table 的最后一行
onclick delete will always delete the last row of the table
我在 while 循环中有这个。每次我确认任何 table 行的 onclick 删除时,它总是删除最后一行。
echo '<td><a href="#openModal'.$id.'" data-toggle="modalDialog" data-target="#openModal"><img src=img/view.png width=20px height=20px id="view"></a></td>';
echo '<td><a href="#" onclick="deleteShit()"><img src="img/delete.png" width=20px height=20px name="delete"></a></td></tr>';
?>
<script type="text/javascript">
function deleteShit()
{
if (confirm('Delete?'))
window.location='delete.php?id=<?php echo $id;?>'
}
这是 delete.php 页面
$id=$_GET['id'];
$query = "UPDATE `main` SET status=0 where id = $id";
$sql = $db->prepare($query);
if ($sql->execute()) {
echo "<script>
window.alert('Item deleted!')
window.location='view.php';
</script>";
}
你必须在 while 之外编写你的 javascript 函数,并带有一个 arg :
<script type="text/javascript">
function deleteShit(id)
{
if (confirm('Delete '+id+'?')) {
window.location = 'delete.php?id='+id;
}
}
</script>
并使用 arg 调用函数:
echo '<td><a href="#" onclick="deleteShit(\''.$id.'\')"><img src="img/delete.png" width=20px height=20px name="delete"></a></td></tr>';
我在 while 循环中有这个。每次我确认任何 table 行的 onclick 删除时,它总是删除最后一行。
echo '<td><a href="#openModal'.$id.'" data-toggle="modalDialog" data-target="#openModal"><img src=img/view.png width=20px height=20px id="view"></a></td>';
echo '<td><a href="#" onclick="deleteShit()"><img src="img/delete.png" width=20px height=20px name="delete"></a></td></tr>';
?>
<script type="text/javascript">
function deleteShit()
{
if (confirm('Delete?'))
window.location='delete.php?id=<?php echo $id;?>'
}
这是 delete.php 页面
$id=$_GET['id'];
$query = "UPDATE `main` SET status=0 where id = $id";
$sql = $db->prepare($query);
if ($sql->execute()) {
echo "<script>
window.alert('Item deleted!')
window.location='view.php';
</script>";
}
你必须在 while 之外编写你的 javascript 函数,并带有一个 arg :
<script type="text/javascript">
function deleteShit(id)
{
if (confirm('Delete '+id+'?')) {
window.location = 'delete.php?id='+id;
}
}
</script>
并使用 arg 调用函数:
echo '<td><a href="#" onclick="deleteShit(\''.$id.'\')"><img src="img/delete.png" width=20px height=20px name="delete"></a></td></tr>';