Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
提交php表单后显示flash消息_Php_Forms_Redirect_Notifications_Flash Message - Fatal编程技术网

提交php表单后显示flash消息

提交php表单后显示flash消息,php,forms,redirect,notifications,flash-message,Php,Forms,Redirect,Notifications,Flash Message,我想在用php提交表单后显示一些通知。 我不知道我做的是否正确。。。 我想提交我的表单,刷新表单中的数据并显示通知 例如,这是我的添加函数: 公共函数addNewPost(){ $manager=newpostsmanager($this->db); 如果(isset($_POST['publish'])){ 如果(空($_POST['title'])空($_POST['header']))|空($_POST['author']))|空($_POST['content'])) { $\u会话['

我想在用php提交表单后显示一些通知。 我不知道我做的是否正确。。。 我想提交我的表单,刷新表单中的数据并显示通知

例如,这是我的添加函数:

公共函数addNewPost(){
$manager=newpostsmanager($this->db);
如果(isset($_POST['publish'])){
如果(空($_POST['title'])空($_POST['header']))|空($_POST['author']))|空($_POST['content']))
{
$\u会话['AddPostData']=$\u POST;
$session=新会话();
$session->setFlash(“‘标题’、‘标题’、‘作者’和“内容是必需的,不能为空”);
$session->flash();
标题('Location:'.$\u SERVER['REQUEST\u URI']);
}
其他的
{ 
$newpost=新职位([
'title'=>$\u POST['title'],
'header'=>$\u POST['header'],
'author'=>$\u POST['author'],
“日期”=>日期(“Y-m-d H:i:s”),
'content'=>$\u POST['content'],
'featuredImg'=>$this->uploadImg()
]); 
$manager->add($newpost);//创建新帖子
取消设置($_会话['addPostDatas']);
标题('Location:index.php?p=blog');
$session=新会话();
$session->setFlash('post已发布!','success');
$session->flash();
}
}
}
在我的情况下,没有显示任何内容,我不知道该怎么做

我的课程是这样的:

课堂会话{
公共函数构造(){
如果(!isset($_会话)){
会话_start();
} 
}
公共函数setFlash($message,$type='danger'){
$\u会话['flash']=数组(
'message'=>$message,
'type'=>$type
);
}
公共功能flash(){
如果(isset($_会话['flash'])){
?>

为了检查会话是否已启动,能否更改代码

public function __construct(){
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
}
我认为$\u会话全局变量总是默认设置的

UPD.

正如我所注意到的,您首先刷新消息,然后重定向页面

UPD2

快速回顾之后,我认为您的addNewPost()方法一定是这样的

public function addNewPost()
{
    $session = new Session();
    $manager = new PostsManager($this->db);
    if (isset($_POST['publish'])) {
        if (empty($_POST['title']) || empty($_POST['header']) || empty($_POST['author']) || empty($_POST['content'])) {
            $_SESSION['addPostDatas'] = $_POST;
            $session->setFlash('"Title", "Header", "Author" and "Content are required and cannot be empty"');
            header('Location: ' . $_SERVER['REQUEST_URI']);
            exit();

        } else {
            $newpost = new Post([
                'title' => $_POST['title'],
                'header' => $_POST['header'],
                'author' => $_POST['author'],
                'date' => date("Y-m-d H:i:s"),
                'content' => $_POST['content'],
                'featuredImg' => $this->uploadImg()
            ]);
            $manager->add($newpost); // Create a new post
            unset($_SESSION['addPostDatas']);
            $session->setFlash('The post was published !', 'success');
            header('Location: index.php?p=blog');
            exit();

        }
    } else {
        $session->flash();
    }
}

请注意,您必须调用$session=new session();$session->flash();当您处理成功请求(index.php?p=blog)

以检查会话是否已启动时,是否可以更改代码

public function __construct(){
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
}
我认为$\u会话全局变量总是默认设置的

UPD.

正如我所注意到的,您首先刷新消息,然后重定向页面

UPD2

快速回顾之后,我认为您的addNewPost()方法一定是这样的

public function addNewPost()
{
    $session = new Session();
    $manager = new PostsManager($this->db);
    if (isset($_POST['publish'])) {
        if (empty($_POST['title']) || empty($_POST['header']) || empty($_POST['author']) || empty($_POST['content'])) {
            $_SESSION['addPostDatas'] = $_POST;
            $session->setFlash('"Title", "Header", "Author" and "Content are required and cannot be empty"');
            header('Location: ' . $_SERVER['REQUEST_URI']);
            exit();

        } else {
            $newpost = new Post([
                'title' => $_POST['title'],
                'header' => $_POST['header'],
                'author' => $_POST['author'],
                'date' => date("Y-m-d H:i:s"),
                'content' => $_POST['content'],
                'featuredImg' => $this->uploadImg()
            ]);
            $manager->add($newpost); // Create a new post
            unset($_SESSION['addPostDatas']);
            $session->setFlash('The post was published !', 'success');
            header('Location: index.php?p=blog');
            exit();

        }
    } else {
        $session->flash();
    }
}


请注意,当您处理成功请求(index.php?p=blog)时,必须调用$session=new session();$session->flash();

您是否正在处理其他人的代码库?或者这是基于某个框架的?不,这是我的工作,我只是在使用twig进行模板制作。嗯,您有点试图做一个单例(用于会话)。您可能希望一直使用它。这将很难提供帮助,因为代码不是公开的,但其中大部分没有显示(任何类的其余部分都有方法
addNewPost
PostsManager
Post
,整个路由机制等等。我能说的是有很多奇怪之处。例如,您似乎有一个
会话
类,但偶尔也会访问
$\u会话
。您试图显示消息并按在相同的方法中形成重定向(只会发生一个)等等。在会话中添加内容和在相同的页面加载期间运行它有什么意义?你是在处理其他人的代码库吗?还是基于某个框架?不,这是我的工作,我只是使用twig进行模板化。嗯,你有点想做一个单例(用于会话)。您可能希望一直使用它。这将很难提供帮助,因为代码不是公开的,但其中大部分没有显示(任何类的其余部分都有方法
addNewPost
PostsManager
Post
,整个路由机制等等。我能说的是有很多奇怪之处。例如,您似乎有一个
会话
类,但偶尔也会访问
$\u会话
。您试图显示消息并按在相同的方法中形成重定向(只会发生一个)等等。在会话中添加内容和在相同的页面加载期间运行它有什么意义?我在构造中替换了它,它仍然没有显示我的通知。是的,谢谢,我更改了顺序(设置notif,然后重定向),但它不起作用。在任何键中,您必须调用会话类,您是在视图中还是在任何地方调用的?类似于这样的if(isset($_POST['publish']){….}else{$Session=new Session();$Session->flash();}不,我没有,我正在使用我的两个方法setFlash()和flash()直接在我的控制器中…是的,我确信这对于一个如此深刻的答案来说实在太深了,但不知何故,我总是试图回答这些类型的问题!我在我的构造中替换了它,它仍然没有显示我的通知。是的,谢谢,我更改了顺序(设置notif,然后重定向),但它不起作用。在任何键中,您必须调用会话类,您是在视图中还是在任何地方调用的?类似于这样的if(isset($_POST['publish']){….}else{$Session=new Session();$Session->flash();}不,我没有,我正在使用我的两个方法setFlash()和flash()直接在我的控制器中…是的,我确信这对于一个如此深刻的答案来说实在太深了,但不知何故,我总是试图回答这类问题!