如何显示会话警报,重新加载页面并显示警报?

问题描述

我正在为我的PHP应用程序创建一个简单的警报系统,但似乎无法弄清为什么不显示警报。 我正在为应用程序使用MVC模型。

我正在使用session_start()

代码

    <?PHP
    function display_notifications(){
      $types = ['error','success','info'];
    
        foreach($types as $type) {
            if(isset($_SESSION[$type]) && !empty($_SESSION[$type])) {
                if(!is_array($_SESSION[$type])) $_SESSION[$type] = [$_SESSION[$type]];
    
                foreach($_SESSION[$type] as $message) {
                    $csstype = ($type == 'error') ? 'error' : $type;
    
                    echo '
                        <script>
                            const Toast = Swal.mixin({
                            toast: true,position: "top-end",showConfirmButton: false,timer: 3000,timerProgressBar: true,didOpen: (toast) => {
                                toast.addEventListener("mouseenter",Swal.stopTimer)
                                toast.addEventListener("mouseleave",Swal.resumeTimer)
                            }
                            })
    
                            Toast.fire({
                            icon: "'. $csstype .'",title: "'. $message .'"
                            })
                        </script>
                    ';
                }
                unset($_SESSION[$type]);
            }
        }
    }
    ?>

我在一个视图文件调用上述函数,然后在控制器中刷新页面,希望在页面刷新后显示警报。下面是我使用的代码示例。

index.PHP(视图)

    <?PHP
    display_notifications();
    ?>

IndexController(控制器)

    <?PHP
    
    $_SESSION['success'][] = "Success!";
    refresh();
    
    ?>

编辑: 在刷新页面之前,会话是否未设置?如果是这样,我将如何解决/改善此问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)