JavaScript 确认 PHP 删除按钮

Phi*_*nna 3 html javascript php alert

当用户单击删除按钮时,如何添加 JavaScript 警报以确认(是或否)?我尝试向警报添加一个类:

<?php
//$con = mysqli_connect("localhost", "root", "root", "db");
$sql = "SELECT * FROM `uploads` where userId = " . $_SESSION['user'];
$qry = mysqli_query($conn,$sql) or die(mysqli_error($conn));

$table_content = "";

while($row = mysqli_fetch_assoc($qry)){
    $id = $row['id'];
    $name = $row['name'];
    $table_content .= "<tr>
                         <td>
                           <a href='listen.php?id=$id' target='_new'>$name </a>
                         </td>
                         <td>
                           <a href='delete.php?id=$id' type='button' class='btn btn-danger'>delete</a>
                         </td>
                       </tr>";
}

echo "<table>".$table_content."</table>";
?>
Run Code Online (Sandbox Code Playgroud)

Lek*_*ens 6

为简单起见,请检查一下。

<a onClick="return confirm('Are you sure you want to delete?')" href='delete.php?id=$id' type='button' class='btn btn-danger'>delete</a>
Run Code Online (Sandbox Code Playgroud)

当你调用它时,return false 实际上是在做三件非常独立的事情:

  • event.preventDefault();
  • event.stopPropagation();
  • 停止回调执行并在调用时立即返回。

由于confirm方法返回true 或false,我们可以简单地在onclick 上调用它,然后返回确定操作。

参考:kamesh 答案

<a onClick="return confirm('Are you sure you want to delete?')" href='delete.php?id=$id' type='button' class='btn btn-danger'>delete</a>
Run Code Online (Sandbox Code Playgroud)