Php 用户通知

Php 用户通知,php,javascript,html,notifications,Php,Javascript,Html,Notifications,对用户通知技术很好奇。指的是简单的东西。例如,用户提交了一个联系人表单。提交后,用户会看到一条消息,表示表单提交成功 问题是。(使用PHP)如何在不向url追加字符串的情况下实现这一点?我这样问是因为我看到越来越多的网站通知用户而不在url中使用GET查询变量 他们只是将某个东西存储在某个全局变量中,然后读取该值/读取时不确定吗?目前使用了哪些技术来实现这一点 在发布和保存表单时进一步添加: //The form has been processed, emailed, etc. Now red

对用户通知技术很好奇。指的是简单的东西。例如,用户提交了一个联系人表单。提交后,用户会看到一条消息,表示表单提交成功

问题是。(使用PHP)如何在不向url追加字符串的情况下实现这一点?我这样问是因为我看到越来越多的网站通知用户而不在url中使用GET查询变量

他们只是将某个东西存储在某个全局变量中,然后读取该值/读取时不确定吗?目前使用了哪些技术来实现这一点

在发布和保存表单时进一步添加:

//The form has been processed, emailed, etc. Now redirect the user back to the page
//We redirect the user so that if they click refresh, the form doesn't re-submit.
//So without a query var in the url, who does the page no that a form was saved?
//Sessions are one solution, what if sessions are not being used?
header("Location:the_original_page.php");

您可以将其存储在会话变量中,该变量在检索数据时被清除

例如:

// when the form is processed successfully;

session_start();

$_SESSION['message'] = "Thank you for signing up";

// the redirect;
在“谢谢”页面中,您可以访问变量

session_start();
if (isset($_SESSION['message'])){
   $message = $_SESSION['message'];
   unset($_SESSION['message']); // delete the message
}

现在您可以使用message变量查看消息了

如果我正确理解了这个问题,他们可能正在使用ajax

一种叫做POST:)的惊人技术

他们通常会设置一个会话变量来指示POST成功,并在下一页加载时检查它:

// After handling the $_POST:
// Assume all data validated and inserted already and you have 
// set the variable $post_was_successful....
session_start();
if ($post_was_successful) {
  $_SESSION['post_success'] = TRUE;
}


// Upon loading the next page:
session_start();
if ($_SESSION['post_success']) {
  echo "you successfully submitted the form.";

  // Then remove the session variable so it isn't reused
  unset($_SESSION['post_success']);
}
else {
  // Whatever you need to say if it was a failure
  // or reload the form for resubmission or whatever..
}

这需要某种持久性存储。如果没有使用会话(它们可能应该使用),则您需要在加载页面时检查数据库,以查看是否存在适当的信息,从而完成相同的任务。

通常在保存功能中,您会执行以下操作:

if(thingWasSaved) {
  NotifyUser("Your Thing was saved, awesome")
} else {
  NotifyUser("There was a problem saving your thing")
}
您也可以对try-catch语句等执行相同的操作。

简单:cookies(或会话)。Cookie实际上更容易,因为它们的值只在后续请求中填充,它们不会占用您的服务器空间,您也不必依赖会话

这种延迟的消息通常被描述为flash消息


我喜欢的一个具体实现是。

使用类并全局使用它。另外,将表单发布为空(action=“”),这样您就可以在确定要发送的meessage之前验证并保存/发送信息,然后将其发送给Notifier类

然后在文档中的每一页上都有打印代码

例如


姓名:
电邮:
通过这种方式,您可以做各种很酷的事情:表单验证、粘性输入、错误输入上的红色标签字段、提交时的表单隐藏以及所有事情的通知。以下是通知类的示例:

<?
class Notifier{

    public static $instance;
    private $messages = array();

    private function __construct(){
    }

    public static function getInstance(){
        if(!isset(self::instance)){
            self::instance = new Notifier();
        }
        return self::instance;
    }

    public function add($message, $class){
        $this->messages[] = '<p class='.$class.'>'.$message.'</p>';
    }

    public function _print(){
        if(!is_array($this->messages)){
            $this->messages = array($this->messages);
        }

        if(count($this->messages)){
            foreach($this->messages as $msg){
            print $msg;
        }
    }
}

显然。但是表单提交后,会发生重定向,将用户发送回原始页面。那么,发帖时通过POST发送成功消息的“技巧”是什么呢?我不认为这就是问题的含义。听起来OP在querystring中看到了类似于
/page?success=1
的东西来表示成功。@Michael,在这种情况下URL中没有GET var。@克里斯:这不是你的问题-我以为你只是指表单提交页面上的成功消息。在这种情况下,它可以是POST,但更可能是会话变量。POST?没有以任何方式回答这个问题AJAX会使它变得简单:)但并不是所有这些网站都在使用AJAX。这是真的。也许是最好的方法。但是如果你没有在你的网站上使用会话呢?比如Wordpress网站。你的意思是通过ajax发送数据吗?是的。如果您没有使用会话怎么办?@Chris如果您没有使用会话,并且需要在页面加载期间保持状态,那么您必须使用querystring参数,如您的问题所示!。如果必须在整个页面加载过程中保持这种状态,那么会话是必要的。我想如果post/submission无法通过,他可能希望在其中使用else命令。根据他如何成功地构建这些页面等等,不确定他想把这些放在哪里。但让一个通知程序说提交也失败可能是明智的。@Chris实际上我想你也可以在下一个页面加载时检查数据库,看看是否成功,但使用会话变量几乎肯定会更容易、更有效。@JackMcE我想这是暗示,但是我添加了一个
else
来澄清问题。请参阅我的答案中添加的注释,说明如何使用数据库而不是会话来确定是否成功。我喜欢这个选项。另外,您可以在服务器上使用PHP设置cookie,如果需要,还可以在客户端使用Javascript读取cookie。这可能是迄今为止最好的解决方案。尽管数据库选项有效,但这似乎有点资源密集型。
<?
class Notifier{

    public static $instance;
    private $messages = array();

    private function __construct(){
    }

    public static function getInstance(){
        if(!isset(self::instance)){
            self::instance = new Notifier();
        }
        return self::instance;
    }

    public function add($message, $class){
        $this->messages[] = '<p class='.$class.'>'.$message.'</p>';
    }

    public function _print(){
        if(!is_array($this->messages)){
            $this->messages = array($this->messages);
        }

        if(count($this->messages)){
            foreach($this->messages as $msg){
            print $msg;
        }
    }
}
<?php
/// Notifier 3.0
class Notifier {

private function __construct() {
}

public static function add($message, $class = NULL) {
    $message_node = '<div class="notification'.(NULL !== $class ? ' '.$class : '').'"'.($this->click_to_dismiss ? ' onclick="this.style.display=\'none\'"' : '').'>'.$message.'</div>';
    $_SESSION[SESSION_NAMESPACE][SESSION_MESSAGES][] = $message_node;
}

public static function print() {
    while(!empty($_SESSION[SESSION_NAMESPACE][SESSION_MESSAGES])) {
        $message = array_shift($_SESSION[SESSION_NAMESPACE][SESSION_MESSAGES]);
        print $message;
        $message = NULL;
    }
}

private function _session_start() {
    if (!headers_sent()) {
        if(session_id() == "") {
            session_start();
        }
    }
}
}
?>