简体   繁体   English

如何让错误消息仅在发送 $_POSTS 后出现?

[英]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.

相关问题 为什么我每次都会收到我已发送标题的错误消息 - Why i get everytime the error-message that i've already sent the headers 查询没有返回结果时,如何显示错误消息? - How can I display an error-message when there are no results returned from the query? 如果没有帖子可以显示,我该如何发送消息 - How do I throw a message if there are no posts to display 提交后如何在弹出窗口中而不是其他页面上显示消息 - how do I get the message after submit to appear inside the pop up instead of on a different page 登录脚本中的PHP crypt()函数返回错误消息 - PHP crypt()-function within login script returns error-message 为什么我在本地环境(仅在原始服务器上)上的Wordpress博客帖子上收到错误消息? - Why do I get an error message on Wordpress blog posts (only) on my local environment (and on the original server I don't) Symfony 3.4 + FOSUserBundle为新的自定义字段添加错误消息 - Symfony 3.4 + FOSUserBundle add Error-Message for new custom Field 发送电子邮件后如何使php重定向? - How can I make php redirect after email message was sent? 填写表格后如何显示“消息发送成功” - How can I display the “Message sent successful” after completing the form 我如何只显示特定类别的帖子? - How do i only display posts with a certain category?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM