在博客上,我正在编码,管理员可以编辑现有帖子。
我想让error
消息出现,例如$_POST['title']
为空(将会显示:“您的帖子应该有标题”)。如果副标题,内容或类别为空,我也会这样做。
如果其中一个或多个为空,则错误工作正常。一旦我加载页面以编辑帖子,就会从头开始显示每个错误。
如何使它们仅在单击$_POST's
之后一个或某些<input type="submit .../>
为空时才显示(当网站加载时它们不应该存在)?
这是PostsAdminController.php
中的功能,用于检查$_POST's
并呈现网站:
public function edit()
{
$error = "";
$id = $_GET['id'];
$entry = $this->postsRepository->find($id);
$categoryFId = $this->categoryRepository->getOneCatFromId($entry->c_Id);
$savedSuccess = false;
$abort = false;
if ($this->loginService->check()) {
if (!empty($_POST['title'])) {
$entry->title = $_POST['title'];
} else {
$error .= "Your post should have a title.";
$abort = true;
}
if (!empty($_POST['subheading'])) {
$entry->subheading = $_POST['subheading'];
} else {
$error .= "A good subheading is nothing you should just leave out.";
$abort = true;
}
if (!empty($_POST['content'])) {
$entry->content = $_POST['content'];
} else {
$error .= "Your post should have content, you know, it wouldn't be a 'post' then.";
$abort = true;
}
if (!empty($_POST['category'])) {
$entry->c_Id = $_POST['category'];
}
if ($abort == false){
$this->postsRepository->update($entry);
$savedSuccess = true;
}
} else {
$error = "You have no permission to do this, how the hell did you get here?";
}
$this->render("post/admin/edit", [
'entry' => $entry,
'error' => $error,
'savedSuccess' => $savedSuccess,
'categoryFId' => $categoryFId
]);
}
我真的希望有人可以帮助我解决这个问题,我不知道该怎么做,让他们仅在POSTS已发送后消失。
答案 0 :(得分:0)
您必须检查是否有使用POST操作:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
就您而言
...
if ($this->loginService->check()) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!empty($_POST['title'])) {
...
}
}
}