javascript - PHP - 无法同时注销并显示警报消息
问题描述
我有一个 PHP 登录系统,如果您愿意,您可以在其中删除您的帐户/个人资料。当用户单击我希望他们注销的按钮时,他们的帐户将被删除,并且会显示一条警告消息 (JavaScript),说明他们的帐户已被删除。问题是当我包含 logout.php 文件时没有出现警报消息。如果我删除该行代码,则会出现警报消息,但用户仍将登录。我该如何解决这个问题?
这是我的代码。
配置文件.php:
<form class="form" action="delete.php" method="post">
<button type="submit" name="delete" value="delete" onclick="return confirm('Are you sure you want to delete your profile?')">Delete profile</button>
</form>
删除.php:
<?php
session_start();
if (isset($_POST["delete"])) {
require_once 'dbh.inc.php';
$deleteUser = "DELETE FROM users WHERE usersId='".$_SESSION["userid"]."'";
if ($conn->query($deleteUser) === TRUE) {
echo "<script>alert('The profile has been deleted.');</script>";
include 'logout.php';
} else {
echo '<script>alert("Something went wrong: ")</script>';
}
} else {
header("location: ../index.php");
exit();
}
注销.php:
<?php
session_start();
session_unset();
session_destroy();
header("location: ../index.php");
exit();
解决方案
在 js 中添加重定向alert()
而不是在 php 中执行。
echo "<script>alert('The profile has been deleted.'); window.location.href='logout.php';</script>";
这将显示警报然后重定向到 logout.php
推荐阅读
- javascript - 使用 javascript e.preventDefault() 时从 request.POST 中获取对象实例 ID
- hive - HDP 3.1.5 | Hive 创建角色问题
- android - 仅发送数据时不调用 onMessageReceived
- git - 未触及的文件被添加到 git 中的未跟踪部分
- c# - 它是否存在一种从 Winforms C# 中的用户控件打开表单和关闭父表单的方法?
- javascript - 比较数组Array的元素并计算元素的频率
- javascript - 为什么我的空白输入检查工具提示不起作用?
- java - 不选择如何传递微调器值
- javascript - For-Of 循环与 For 循环
- node.js - Express 和 Nginx 解决路由问题