How to show Success Message / Flash message in PHP

In this article, we will learn how to show success message after completing a task. You will learn how to use sessions in php and use sessions to set the flash message and display them after success.

Note : To use session in any page, we need to start the session on top of the page using the session_start();

After inserting / updating the data in the database, set the session message in a session variable as shown below:

<?php 
    session_start();

    //after insert or update 
    $_SESSION['status'] = "<Type Your success message here>";
?>

Now we have set the message and now paste the below code in the page where you want display the success message :

Note : Import the Bootstrap v5 CSS and JS using the cdn link available on getboostrap.com.

<?php 
    session_start();
    
    if(isset($_SESSION['status']))
    {
        ?>
            <div class="alert alert-warning alert-dismissible fade show" role="alert">
                <strong>Hey !</strong> <?= $_SESSION['status']; ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        <?php 
        unset($_SESSION['status']);
    }

?>


Thanks for reading ;-)

© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT