php flashmessage using sweetalert and bootstrap in oop
Welcome to a new article today we will learn how to use flashmessage in php using object oriented programming as well as we will learn how to use sessions in php.
FlashmessageClass.php:This file includes the skcoder library for flashmessage.
Index.php: This is the home page of the project.
Setflashmessage.php: This is a page where you will configure messages and choose their types.
Step1 : code of ssp.class.php libirary.
Well, let's explain the script in steps:
Step 1: We create a folder in localhost with the name flashmessage as we used to use xamp so we will put the folder in htdocs and put the files in the above order in the file structure with the same names.
Step 2 We put the following in FlashmessageClass.php And this code is flashmessage library There are some important
functions for using this library, which is the.
setMessage():The usefulness of this function is to configure the message and choose its type.
printMessage(): function, the usefulness of this function is to print the message stored in the session.
FlashmessageClass.php:
<?php
class flashMessage
{
/**
* class create by skmcoder
* create in 2020/11/3 8:04pm
*/
//Message type
private $messageType;
//the message
private $message;
//I am the standard messages
public function sweetalert($typeicon, $message, $timer = 2500)
{
return " Swal.fire({
position: 'top-end',
icon: '$typeicon',
title:'$message' ,
showConfirmButton: false,
timer: $timer
});";
}
private const standrMessageType = array("success", "warning", "primary", "danger", "info", "light", 'dark');
/* Call the main function */
/**
* setMessage function
*
* @param [string] $message
* @param [string] $messageType
* @return void
*/
public function setMessage($message, $messageType)
{
$this->message = $message;
$this->messageType = $messageType;
$this->messageOption();
}
/* This function is responsible for selecting the message type */
public function messageOption()
{
switch ($this->messageType) {
case "success":
$this->success();
break;
case "warning":
$this->warning();
break;
case "question":
$this->question();
break;
case "error":
$this->error();
break;
case "info":
$this->info();
break;
};
}
/* I am messages */
/**
* Undocumented function
*
* @return void
*/
private function success()
{
$_SESSION['success_message'] = $this->sweetalert('success', $this->message);
}
private function warning()
{
$_SESSION['warning_message'] = $this->sweetalert('warning', $this->message);
}
private function question()
{
$_SESSION['question_message'] = $this->sweetalert('question', $this->message);
}
private function error()
{
$_SESSION['error_message'] = $this->sweetalert('error', $this->message);
}
private function info()
{
$_SESSION['info_message'] = $this->sweetalert('info', $this->message);
}
/* I am messages */
/* This function is especially with the message*/
public function printMessage()
{
if (isset($_SESSION['success_message'])) {
echo $_SESSION['success_message'];
$_SESSION['success_message'] = '';
}
if (isset($_SESSION['warning_message'])) {
echo $_SESSION['warning_message'];
$_SESSION['warning_message'] = '';
}
if (isset($_SESSION['question_message'])) {
echo $_SESSION['question_message'];
$_SESSION['question_message'] = '';
}
if (isset($_SESSION['error_message'])) {
echo $_SESSION['error_message'];
$_SESSION['error_message'] = '';
}
if (isset($_SESSION['info_message'])) {
echo $_SESSION['info_message'];
$_SESSION['info_message'] = '';
}
}
};
?>
Step3 : In this step, we write the home page.
index.php:
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Hugo 0.72.0">
<title>php flashmessage</title>
<link rel="canonical" href="https://v5.getbootstrap.com/docs/5.0/examples/album/">
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css" integrity="sha384-r4NyP46KrjDleawBgD5tp8Y7UzmLA05oM1iAEQ17CSuDqnUK2+k9luXQOfXJCJ4I" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/js/bootstrap.min.js" integrity="sha384-oesi62hOLfzrys4LxRF63OJCXdXDipiYWBnvTl9Y9/TRlw5xlKIEHpNyvvDShgf/" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<style>
.msg-btn {
margin-left: 5px;
}
</style>
</head>
<body style="padding:30px; background-color:#f2f4f6 !important;">
<?php
include 'setflashmessage.php';
?>
<h2 align="center " class="alert alert-success"> php flash message using sweetalert with bootstrap in oop by skmcoder </h2>
<div class="row" style="padding:60px; ">
<div class="col-md-1"></div>
<div class="col-md-10 text-center">
<form action="index.php" method="post">
<button type="submit" name="success" class=" btn btn-success msg-btn">success message</button>
<button type="submit" name="error" class=" msg-btn btn btn-danger">error message</button>
<button type="submit" name="warning" class=" msg-btn btn btn-warning">warning message</button>
<button type="submit" name="question" class=" msg-btn btn btn-primary">question message</button>
<button type="submit" name="info" class=" msg-btn btn btn-info">info message</button>
</form>
</div>
<div class="col-md-1"></div>
</div>
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
<?php
//print flashmessage
$flashMessage->printMessage();
?>
</script>
</body>
</html>
Step4 : In this step, we will write in the setflashmessage.php file the following code and the function of this code is to change the status and types of messages and we call this code on the main page.
setflashmessage.php:
<?php
require 'flashmessageClass.php';
$flashMessage = new flashMessage();
//check message alert type
if (isset($_POST['success'])) {
//setsuccess flash message
$flashMessage->setMessage(" flashmessage success ", 'success');
} else if (isset($_POST['error'])) {
//setdanger flash message
$flashMessage->setMessage(" flashmessage error ", 'error');
} else if (isset($_POST['warning'])) {
$flashMessage->setMessage(" flashmessage warning ", 'warning');
} else if (isset($_POST['question'])) {
$flashMessage->setMessage(" flashmessage question ", 'question');
} else if (isset($_POST['info'])) {
$flashMessage->setMessage(" flashmessage info ", 'info');
}
?>