How to Create a Flash Message using Session PHP Source Code

php,flash message tutorial,php session message tutorial,codeigniter flashdata like,php tutorial for beginners,php tutorials for students,php session,storing temporary messages in php

In this post you will learn about How to Create a Flash Message using Session PHP Source Code. Read this below article carefully and understand this to put impact in real life.

You’ll learn how to use PHP Session to produce Flash Messages in PHP. The Flash Message I’m referring about is similar to the Flashdata Function in CodeIgniter. It enables the developer to save temporary messages or data that may be accessed on the next request and then deleted.

What are Flash Messages and How Do They Work?

The Flash Messages function operates similarly to the Flashdata Function in CodeIgniter, allowing the developer to save process replies, error responses, messages, and other string data. This data will be saved in a PHP Session, and after the data has been called, the Flash Message will be immediately deleted from the session.

Here, we’ll make a basic PHP application with numerous buttons that will store a Flash Message. The system will be returned back to the page referrer and show the message when the flash message has been successfully saved.

The First Steps

To execute our PHP script, install XAMPP as your local web server. Open the XAMPP Control Panel and start the Apache Server after installing the virtual server.

For the UI design of the app that we’ll be making, download Bootstrap v5. Then, on your end, copy the library directory to the location where you’ll keep the source code.

Developing The User Interface

The essential codes for showing flash messages and other displays are contained in the script of our Main Page, which is shown below. This file should be saved as index.php.

<?php require_once('function.php') ?>
<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flash Messages</title>
    <link rel="stylesheet" href="./Font-Awesome-master/css/all.min.css">
    <link rel="stylesheet" href="./css/bootstrap.min.css">
    <script src="./js/bootstrap.min.js"></script>
    <style>
        :root {
            --bs-success-rgb: 71, 222, 152 !important;
        }
 
        html,
        body {
            height: 100%;
            width: 100%;
            font-family: Apple Chancery, cursive;
        }
 
        .btn-info.text-light:hover,
        .btn-info.text-light:focus {
            background: #000;
        }
    </style>
</head>
 
<body class="bg-light">
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark bg-gradient" id="topNavBar">
        <div class="container">
            <a class="navbar-brand" href="https://sourcecodester.com">
            Sourcecodester
            </a>
 
            <div>
                <b class="text-light">Flash Messages</b>
            </div>
        </div>
    </nav>
    <div class="container py-5" id="page-container">
        <h4 class="text-center"><b>Flash Messages Like CodeIgniter's Flashdata Functions.</b></h4>
 
        <!-- Displaying Flash Data if exist -->
        <?php if(isset($_SESSION['_flashdata'])): ?>
            <!-- Looping All Flash messages -->
            <?php foreach($_SESSION['_flashdata'] as $key => $val): ?>
                <div class="my-2 px-3 py2 alert alert-<?= $key ?>">
                    <div class="d-flex align-items-center">
                        <div class="col-11">
                            <!-- Displaying the Flash Message -->
                            <p><?= get_flashdata($key) ?></p>
                        </div>
                        <div class="col-1 text-end">
                            <button class="btn-close" type="button" onclick="this.closest('.alert').remove()"></button>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
            <!-- Looping All Flash messages -->
        <?php endif; ?>
        <!-- Displaying Flash Data if exist -->
 
 
        <div class="col-lg-12 pt-5">
            <div class="row">
                <div class="col-md-3 px-2">
                    <a href="set_messages.php?msg=info" class="btn btn-lg rounded-pill btn-pill btn-info">Show an Info Message</a>
                </div>
                <div class="col-md-3 px-2">
                    <a href="set_messages.php?msg=success" class="btn btn-lg rounded-pill btn-pill btn-success">Show a Success Message</a>
                </div>
                <div class="col-md-3 px-2">
                    <a href="set_messages.php?msg=error" class="btn btn-lg rounded-pill btn-pill btn-danger">Show an Error Message</a>
                </div>
                <div class="col-md-3 px-2">
                    <a href="set_messages.php?msg=multiple" class="btn btn-lg rounded-pill btn-pill btn-default border">Show a Multiple Message</a>
                </div>
            </div>
        </div>
    </div>
 
 
</body>
 
</html>

Developing a Process File

The sample PHP file that records the flash messages and redirects to the Main Page is shown below. Set the file’s name to set_messages.php.

<?php require_once('function.php') ?>
<?php
if(!isset($_GET['msg'])){
    echo "<script> alert('No message to set.'); location.href='./'</script>";
}
 
$msg  = $_GET['msg'];
 
switch ($msg){
    case 'info';
        set_flashdata('info',"<i class='fa fa-info-circle'></i> This is a Sample Info Message.");
    break;
    case 'success';
        set_flashdata('success',"<i class='fa fa-check'></i> This is a Sample Success Message.");
    break;
    case 'error';
        set_flashdata('danger',"<i class='fa fa-exclamation-triangle'></i> This is a Sample Error Message.");
    break;
    case 'multiple';
        set_flashdata('info',"<i class='fa fa-info-circle'></i> This is a Sample Info Message.");
        set_flashdata('success',"<i class='fa fa-check'></i> This is a Sample Success Message.");
        set_flashdata('danger',"<i class='fa fa-exclamation-triangle'></i> This is a Sample Error Message.");
    break;
    default:
        set_flashdata('danger',"Undefined Flash Message.");
    break;
}
header('location:./');

Done!

Final Words

In this post you learnt about How to Create a Flash Message using Session PHP Source Code. If you like this article please share with your friends and family to support our website. We wrote this article by researching on internet to find best content for you. You can find more articles like this on our website for free. We provided you some important tips regarding this topic by following them you can easily understand. If you need more information regarding How to Create a Flash Message using Session PHP Source Code you can ask in comments or you can contact us via contact us form available on this website. Thanks for reading and sharing.


2 Comments on “How to Create a Flash Message using Session PHP Source Code”

  1. I was very happy to discover this website. I want to to thank you for your time due to this fantastic read!! I definitely liked every little bit of it and i also have you book marked to check out new things on your blog.

Leave a Reply

Your email address will not be published. Required fields are marked *