我想在php中提交表单后显示一些通知。 我不知道我是否采取了正确的方式...... 我想提交表单,在表单中刷新我的数据并显示通知。
例如,这是我的添加功能:
prepareForReuse()
在我的情况下,没有显示任何内容,我不知道该怎么做。
我的会话类是这样的:
public function addNewPost(){
$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 = new Session();
$session->setFlash('"Title", "Header", "Author" and "Content are required and cannot be empty"');
$session->flash();
header('Location: '.$_SERVER['REQUEST_URI']);
}
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']);
header('Location: index.php?p=blog');
$session = new Session();
$session->setFlash('The post was published !', 'success');
$session->flash();
}
}
}
你能告诉我这样做的方法吗?
非常感谢!
修改
这是我的index.php
Class Session{
public function __construct(){
if(!isset($_SESSION)){
session_start();
}
}
public function setFlash($message, $type = 'danger'){
$_SESSION['flash'] = array(
'message' => $message,
'type' => $type
);
}
public function flash(){
if(isset($_SESSION['flash'])){
?>
<div id="alert" class="alert alert-<?php echo $_SESSION['flash']['type'] ?>" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong><?php print_r($_SESSION['flash']['message']); ?></strong>
</div>
<?php
unset($_SESSION['flash']);
}
}
}
我的观点是这样的
require 'vendor/autoload.php';
use natinho68\Models\SPDO as SPDO;
use natinho68\Controllers\Session as Session;
use natinho68\Models\PostsManager as PostsManager;
use natinho68\Controllers\MailController as MailController;
use natinho68\Controllers\Controller as Controller;
$db = new SPDO();
$session = new Session();
$posts = new PostsManager($db);
$contact = new MailController();
// Routing
$page = "home";
if(isset($_GET['p'])){
$page = $_GET['p'];
}
// Rendu du template
// Chargement des templates dans le dossier templates
$loader = new Twig_Loader_Filesystem(__DIR__ . '/Views');
$twig = new Twig_Environment($loader, [
'cache' => false, // __DIR__ . '/tmp'
'debug' => true
]);
$twig->addExtension(new Twig_Extension_Debug());
if(!empty($_SESSION['addPostDatas'])){
$twig->addGlobal('addPostDatas', $_SESSION['addPostDatas']);
}
$controller = new Controller($twig, $db);
switch ($page){
case 'home' :
$controller->home('home.twig');
$contact->mailer();
break;
case 'contact' :
echo $twig->render('contact.twig');
$contact->mailer();
break;
case 'blog':
echo $twig->render('allPosts.twig', ['allPosts' => $posts->getAllPosts()]);
break;
case 'singlepost' :
$controller->showPost($_GET['id'], 'singlePost.twig');
break;
case 'editpost' :
$controller->showPost($_GET['id'],'editpost.twig');
$controller->updatePost();
$controller->deletePost();
break;
case 'add-post' :
$controller->addPostView('addPost.twig');
$controller->addNewPost();
break;
default:
header('HTTP/1.0 404 Not Found');
echo $twig->render('404.twig');
break;
}
答案 0 :(得分:2)
为了检查会话是否已启动,您可以更改代码吗?
public function __construct(){
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
}
我认为$ _SESSION全局变量总是默认设置。
<强> 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(); $会话而&GT;闪速();处理成功请求时(index.php?p = blog)