[英]How do I let an error-message only appear after the $_POSTS are sent?
On a blog I'm coding the admin can edit existing posts.在我编写的博客上,管理员可以编辑现有帖子。
I want to let an error
-message appear when the $_POST['title']
for eg is empty(There will be displayed:"Your post should have a title").当
$_POST['title']
for eg 为空时,我想让error
消息出现(将显示:“您的帖子应该有一个标题”)。 I also do it if the subheading, content or category are empty.如果副标题、内容或类别为空,我也会这样做。
The errors work just fine if one or some of them is/are empty.如果其中一个或某些错误是空的,则错误工作得很好。 As soon I load the page to edit a post every error is displayed from the beginning.
一旦我加载页面以编辑帖子,每个错误都会从头开始显示。
How do I make them only appear when one or some $_POST's
are empty after the <input type="submit .../>
is clicked (they shouldn't be there when the site has loaded)?我如何让它们只出现在一个或一些
$_POST's
在<input type="submit .../>
被点击后为空时(当网站加载时它们不应该在那里)?
This is the function in the PostsAdminController.php
that checks the $_POST's
and renders the site:这是
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
]);
}
I really hope someone can help me with this, I don't know what I could to to let them only disappear when the POSTS have already been send..我真的希望有人能帮我解决这个问题,我不知道我该怎么做才能让它们只在 POSTS 已经发送后消失..
You have to check if there was a POST action use:您必须检查是否有 POST 操作使用:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
in your case在你的情况下
...
if ($this->loginService->check()) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!empty($_POST['title'])) {
...
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.