2011-06-04 81 views
4

对用户通知技术感到好奇。提到简单的东西。例如,用户提交了一个联系表单。一旦提交,用户就会看到提交表单提交成功的消息。用户通知

这是问题。你怎么做到这一点(与PHP),而不附加一个字符串的网址?我问这是因为我看到越来越多的网站通知用户,而不使用url中的GET查询var。

他们只是将某些东西存储在某个地方的全局变量中,并在读取时读取/取消设置?目前使用什么技术来实现这一点?

为了进一步增加,当表单被提交并保存:

//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"); 
+0

请参阅我的回答中添加的备注关于使用数据库而不是会话来确定发布成功 – 2011-06-04 20:22:47

回答

2

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

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

我喜欢的一个特定实现是Dingo FrameworkNote library

+0

我喜欢这个选项。另外,您可以在服务器上用PHP设置cookie,如果需要,可以在客户端上用Javascript读取。可能是最好的解决方案呢。就像数据库选项一样,这看起来有点资源密集。 – Chris 2011-06-10 01:30:27

1

你可以把它保存在当数据被检索到被清除会话变量

例子:

// 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 
} 

现在你可以使用的消息变量看到消息

+0

这是事实。可能是最好的方法。但是如果你没有在你的网站上使用Sessions呢?比如一个WordPress的网站。 – Chris 2011-06-04 20:04:41

+0

你的意思是说如果你通过ajax发送数据? – Ibu 2011-06-04 20:05:58

0

如果我理解正确的问题,他们可能正在使用AJAX

+0

AJAX会让事情变得简单:)但并非所有这些网站都使用AJAX。 – Chris 2011-06-04 20:04:08

2

一个惊人的技术,称为POST :)

+0

当然+1 .... – dynamic 2011-06-04 20:00:59

+0

显然。但是在提交表单后,发生重定向会将用户发回原始页面。发布回来时,“技术”是否通过POST发送成功消息? – Chris 2011-06-04 20:03:37

+0

我不认为这就是问题所暗示的。听起来像OP在querystring中看到'/ page?success = 1'这样的东西来表示成功。 – 2011-06-04 20:04:22

2

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

// 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.. 
} 

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

+0

确实如此。如果你不使用会话? – Chris 2011-06-04 20:04:57

+0

@Chris如果您没有使用会话,并且您需要状态持续保持页面加载,那么您必须使用查询字符串参数,就像在您的问题中一样!会话是必要的,如果这必须持续一个页面加载。 – 2011-06-04 20:06:07

+0

我想象如果发布/提交无法完成,他会希望在那里有其他命令。根据他如何以成功的结果等方式构建这些网页,他不确定他会在哪里进行调整。但是,让通知者说提交失败也是明智的做法。 – Jem 2011-06-04 20:08:26

2

雅通常在你的保存功能,你会做这样的事情:

if(thingWasSaved) { 
    NotifyUser("Your Thing was saved, awesome") 
} else { 
    NotifyUser("There was a problem saving your thing") 
} 

你也可以做一个try catch语句等一样...

0

使用一个类并在全局范围内使用它。此外,将表单发布为空(action =“”),以便在确定发送消息之前验证和保存/发送信息,然后将其提供给通告程序类。

然后在每个页面上,文档内都有打印代码。

例如。

<? 
require_once('Notifier.class.php'); 
$Notifier = Notifier::getInstance(); 

if($_SERVER['REQUEST_METHOD'] == 'POST'){ 
    // VALIDATE DATA/SAVE DATA/EMAIL DATA 
    if($saved == true){ 
     $Notifier->add('Thank you for your input!', 'success'); 
    } else { 
     $Notifier->add('There was an error!', 'error'); 
    } 
} 

?> 
<html> 
<body> 
<? $Notifier->_print(); ?> 
<form name="contact" method="post" action=""> 
Name: <input type="text" name="name" /> 
Email: <input type="text" name="email" /> 
<input type="submit" value="Save" /> 
</form> 
</body> 

你可以做这种方式做各种很酷的事情:表单验证,粘输入,在输入错误红色素标记的领域,形式隐藏在提交,一切的通知。以下是通知类的示例:

<? 
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(); 
     } 
    } 
} 
} 
?>