Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php echo javascript alert() not working

Tags:

javascript

php

I would like to display a popup message when user is logged out, so I use

echo "<script>alert(\"You are logged out\");</script>";

But it doesn't work.

Below is my coding. Is there any logic problem in my coding?

<?php
session_start();
if(isset($_SESSION['Username']) == "admin")
{
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
@import "../CSS/Style.css";
@import "../CSS/Admin.css";
</style>
<title>Admin Home Page</title>
</head>

<body>
<div class="body"></div>
<?php
    if(isset($_GET['id']) == "logout")
    {
        session_destroy();
        echo "<script>alert(\"You are logged out\");</script>";
        header("Location: ..\Main.php");
    }
    else
    {
?>
<div class="menu">
    <a href="ManageStaff.php">Manage Staff</a>
</div>

<div class="menu2">
    <a href="ManageAccount.php">Manage Account</a>
</div>

<div class="logout">
    <a href="AdminHomePage.php?id=logout">Logout</a>
</div>
<?php
    }
}
else
{
?>
<center>
<p style="font-size:50px; font-weight:bold">Access Denied</p>
<p style="font-size:18px">Your request for this page has been denied because of access control</p>
</center>
<?php
}
?>
</body>
</html>

The session will be destroyed and will also redirect to Main.php, just the alert() will not come out.

like image 401
Newbie Avatar asked Dec 15 '22 15:12

Newbie


1 Answers

You're doing an echo and then writing a relocate header. If you did your relocate in the javascript (after the user clicked the alert), it would probably work the way you expect it to.

echo "<script>alert('You are logged out'); window.location.href='..\Main.php';</script>";

Also, the way that you use isset will cause problems because isset returns true or false (it checks if a value is present), rather than returning the value.

So instead of

if(isset($_SESSION['Username']) == "admin")

You need to do:

if(isset($_SESSION['Username']) && $_SESSION['Username'] == "admin") 
like image 94
Steve K Avatar answered Dec 25 '22 10:12

Steve K