dsrjs86444 2014-10-08 10:40
浏览 63
已采纳

在删除表格行之前显示一个确认框

I have a table whose view is something like this

ID  NAME  LOCATION  DELETE
1   sam   US        delete

I have a statement from the table that deletes the given row

echo "<td><a href=\"delete_members.php?id=".$row['id']."\">Delete</a></td>";

It redirects to delete_members.php page and the row gets deleted, however i wish to display an alert box that makes sure that the user wants to delete the row or not. for it i have a code

<button onclick="myFunction()">Delete</button>

<script>
function myFunction() {
    var x;
    if (confirm("Press a button!") == true) {
        window.location="yourphppage.php"; // not sure which link should be placed here
        return true;
    } else {
        window.location="index.php";
        return true;
    }
    document.getElementById("demo").innerHTML = x;
}
</script>

code for the delete_members.php page

<?php
include('admin_session.php');
$con=mysqli_connect("abc.com","abc","abc","abc");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$id = $_GET['id']; 

mysqli_query($con,"DELETE FROM members WHERE id='".$id."'");
mysqli_close($con);
header("Location: admin_member_list.php");
?> 

However, i am unable to use the confirmation script with the statement in a proper way. i want that the alert box gets popped up(i.e the script runs) after i click on the edit button that is present on the first statement given above and then if the user confirms it should run the delete_members.php script. would appreciate if someone could guide me

  • 写回答

4条回答 默认 最新

  • drjun1994 2014-10-08 10:44
    关注

    In the most simple way:

    echo "<a onclick=\"return confirm('Delete this record?')\" href=\"delete_members.php?id=".$row['id']."\">delete</a>";
    

    you could also do this in an unobtrusive manner, by adding a class to the links and selecting them all at once using something like jQuery and then bind the confirmation logic to the onclick event. Something like this:

    $('a.delete').on('click', function() {
        var choice = confirm('Do you really want to delete this record?');
        if(choice === true) {
            return true;
        }
        return false;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目