Display Message in PHP

PHP Session based flash message means a message which hides after the browser refresh. In this article, you will learn how to create session base flash message.

Display Message in PHP: 

PHP Session based flash message means a message which will hide after the browser refresh same like CodeIgniter framework flash message if you are familiar with PHP Codeigniter framework.  In this article, we will create a simple PHP session base flash message, let's get started.


PHP Session Based Flash Message
PHP Session Based Flash Message

Flash Message Initliaze: 

In an index.php file, we have a button and When someone clicks on the submit button then will create session base message and after will redirect a user to message.php file and there will display the flash message just for 3 seconds after three seconds the message will hide automatically 

index.php



<?php
session_start();

if(isset($_POST['btn'])){

    $_SESSION['message'] = "You have clicked on button successfully";
    header("location:message.php");
}


?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>PHP Flash message</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous"></head>
<body>
    <div class="container">
     <div class="row">
     <div class="col-md-5 mx-auto mt-5">
     <form action="" method="POST">
     <input type="submit" value="Click me" name="btn" class="btn btn-primary">
     </form>
     </div>
     </div>
    </div>
</body>
</html>
In the message.php file, we are displaying the flash message and below the setTimeOut function will hide the alert success message after 3 seconds

message.php


<?php
session_start();

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>PHP Flash message</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous"></head>
<body>
    <div class="container">
     <div class="row">
     <div class="col-md-5 mx-auto mt-5">
     <?php if(isset($_SESSION['message'])): ?>
     <div class="alert alert-success">
     <?php echo $_SESSION['message']; ?>
     </div>
<?php endif; ?>
<?php unset($_SESSION['message']); ?>
     </div>
     </div>
    </div>
    <script>
    
    setTimeout(function() {
        let alert = document.querySelector(".alert");
            alert.remove();
    }, 3000);
    
    </script>
</body>
</html>

COMMENTS

Name

ajax,2,HTML & CSS,19,JavaScript,1,PHP,11,
ltr
item
webtipstricks: Display Message in PHP
Display Message in PHP
PHP Session based flash message means a message which hides after the browser refresh. In this article, you will learn how to create session base flash message.
https://i.ytimg.com/vi/Yxvesc4jjpI/hqdefault.jpg
https://i.ytimg.com/vi/Yxvesc4jjpI/default.jpg
webtipstricks
https://www.webtipstricks.com/2019/02/php-session-based-flash-message.html
https://www.webtipstricks.com/
https://www.webtipstricks.com/
https://www.webtipstricks.com/2019/02/php-session-based-flash-message.html
true
4027471107773963481
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy