我在while循环中有这个。每当我确认任何表行的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>";
}
答案 0 :(得分:1)
你必须用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>';