首页 > 解决方案 > 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();

标签: javascriptphp

解决方案


在 js 中添加重定向alert()而不是在 php 中执行。

echo "<script>alert('The profile has been deleted.'); window.location.href='logout.php';</script>";  

这将显示警报然后重定向到 logout.php


推荐阅读