Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a Modal Window "using" PHP

I am creating a log in system in PHP and I'm trying to make it a little nicer.

When you log out, you get redirected back to index.php. Like this:

header("loaction: index.php?logout=true")

This makes the url look like www.mysite.com/index.php?logout=true. I then use the following code:

if(isset($_GET['logout'])) {

$logoutvalue = $_GET['logout'];

if($logoutvalue = "true") {
$notification = "You've been logged out!";  
}

to get the value of logout from the URL and do something with it.

I have a small popout window that will display the notification variable's value. In this case it's "You've been logged out!" My question is how do I get the modal window to display when the page loads and when the url equals /index.php?logout=true ?

All comments, answers, and suggestions are greatly appreciated!

Thanks! - Ryan

like image 456
Ryan Fitzgerald Avatar asked Jul 03 '26 03:07

Ryan Fitzgerald


1 Answers

First of all,

You can't "Open a Modal Window using PHP" directly. You can only do this only by exchanging variables (via JSON or XML), or embedding PHP conditions right into your markup.

PHP and JavaScript are independent.

My question is how do I get the modal window to display when the page loads and when the url equals /index.php?logout=true ?

There are two ways you can achieve this.
First: Make a good use of embedding PHP conditions right into the markup.

Second: Have somewhere hidden input, like (<input type="hidden" id="showModal" />) and then check if it exists via JavaScript itself.

For example:

<!DOCTYPE html>
<html>
<head>

   <script type="text/javascript">

      window.onload = function(){

           if ( document.getElementById('showModal') ){

               alert('Box'); //replace with your own handler
           }

      }

   </script>

</head>
<body>

<?php if ( isset($_GET['logout']) && $_GET['logout'] === 'true' ): ?>

<input type="hidden" id="showModal" />

<?php endif;?>
</body>
</html>
like image 78
Yang Avatar answered Jul 04 '26 18:07

Yang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!