JavaScript确认PHP删除按钮

时间:2017-04-28 11:04:26

标签: javascript php html 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>";
?>

5 个答案:

答案 0 :(得分:2)

为简单起见,请查看此内容。

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

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

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

由于确认方法返回true或false,我们只需在onclick上调用它,然后返回确定操作。

参考:kamesh answer

&#13;
&#13;
 <a onClick="return confirm('Are you sure you want to delete?')" href='delete.php?id=$id' type='button' class='btn btn-danger'>delete</a>
       
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果你正在使用Bootstrap,你可以给它一个旋转!

https://github.com/delboy1978uk/bs-delete-confirm

如果您没有使用自举,请试一试!一旦你包括css和js,一些很棒的功能就可以随时摇滚!此jQuery删除确认代码使用Bootstrap模式,请参阅此处的示例http://getbootstrap.com/javascript/#modals

<a href="http://nasa.gov" class="delete-confirm btn btn-primary">Go!</a>


$(document).ready(function(){
  $('.delete-confirm').deleteConfirm();
});

查看有效的示例 HERE https://jsfiddle.net/myu3kr66/

答案 2 :(得分:0)

我认为那就是你要找的东西。

<a href='delete.php?id=$id' onclick="return myFunction()" type='button' class='btn btn-danger'>delete</a>

<script>
function myFunction() {
var r = confirm("OK to delete?");
if (r == false) {
   return false;
} 

}
</script>

答案 3 :(得分:0)

你可以使用模态并点击删除按钮,显示模态,    在模态中,保留两个按钮,是和否分别为它们分配id,例如#yes和#no。单击是,执行删除操作,单击否,保持行不变。

<td><a href='listen.php?id=$id' target='_new'>$name </a></td>
<td><a href='delete.php?id=$id' type='button' id="delete"                             

class='btn btn-danger'>delete</a></td>
 </tr>";
}
echo "<table>".$table_content."</table>"

 <script>

$('#delete).on('click',function(){

 $('#modal').show();
 $('#yes').on('click') {
//   perform delete action.
 }
 }
</script>

答案 4 :(得分:0)

请按照

<a href="javascript:void(0)" onclick="return deleteContent('<?php echo $id; ?>');" type="button" class="btn btn-danger">Delete</a>

<强>脚本

<script>
    function deleteContent(id) {
        if(confirm('Are you sure you want to delete this ?')) {
        window.location='delete.php?id='+id;
        }
    return false;
    }
</script>

<强>更新

<?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));

 echo "<table>";
 while($row = mysqli_fetch_assoc($qry)){
      $id = $row['id'];
      $name = $row['name'];
?>
<tr>
<td><a href="javascript:void(0)" onclick="return deleteContent('<?php echo $id; ?>');" type="button" class="btn btn-danger">Delete</a></td>
<td><a href='listen.php?id=<?php echo $id; ?>' target='_new'><?php echo $name; ?> </a></td>
</tr>

<?php } echo "</table>"; ?>