繁体   English   中英

使用ajax和php删除表中的所有行

[英]delete all row in table using ajax and php

我想在使用ajax和php确认后删除表URL,但是它正在删除而没有任何确认。

这是我的代码:

 <form action="admin.php" method="post"> <button type="submit" id="deleteall" name="deleteall" class="btn btn-danger">Delete</button> </form> <?php include 'db.php'; if(isset($_POST["deleteall"])){ $req="DELETE FROM `urls` WHERE 1"; $conn->exec($req); $conn==null; } ?> 
 $(document).ready(function(){ //delea all $(document).on('click', '.deleteall', function(){ var user_id = $(this).attr("id"); if(confirm("Are you sure you want to delete this?")) { $.ajax({ url:"admin.php", method:"POST", data:user_id, }); } else { return false; } }); }); 

您需要将params作为对象发送:

$.ajax({
    url:"admin.php",
    method:"POST",
    data: {user_id: user_id, deleteall: true},
    ...
});

然后在您的admin.php将传递的user_id添加到查询中:

if( isset($_POST["deleteall"]) && isset($_POST["user_id"]) ){

    $req="DELETE FROM `urls` WHERE id=".$_POST["user_id"];
    $conn->exec($req);
    ...
}

希望这可以帮助。

有不同的方法可以实现...这是一种:

<script>

$(document).ready(function(){

    if (confirm("Are you sure you want to delete this?") == true) {
        // here ajax
    } 
}
</script>
<a href="#" onclick="confirmFunction()">Delete</a>

请注意 ,您的查询将删除所有用户的所有内容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM