开发者

User Notification

开发者 https://www.devze.com 2023-03-10 04:58 出处:网络
Curious about user not开发者_运维技巧ification techniques. Referring to simple stuff. For example, a user submit\'s a contact form. Once submitted, the user sees a message which says the form submissi

Curious about user not开发者_运维技巧ification techniques. Referring to simple stuff. For example, a user submit's a contact form. Once submitted, the user sees a message which says the form submission was successful.

Here is the question. How do you accomplish this (with PHP), without appending a string to the url? I ask this because I see more and more sites notifying users without using a GET query var in the url.

Are they just storing something in a global var somewhere and reading that/unsetting on read? What techniques are currently used to accomplish this?

To add further, when the form is posted and saved:

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


Simple: cookies (or sessions). Cookies are actually easier because their values are only populated in the subsequent request and they don't eat up your server space and you don't have to rely on sessions.

This kind of postponed messages are usually described as flash messages.

One particular implementation that I like is the Note library of Dingo Framework.


An amazing technology called POST :)


ya typically in your save function you would do something like:

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

you could also do the same with a try catch statement etc...


They're generally setting a session variable to indicate post success and checking for it when the next page loads:

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

This requires some sort of persistent storage. If sessions are not being used (they probably should be), then you need to accomplish the same thing by checking the database when the page loads to see if the appropriate information is there.


You can store it in a session variable that gets cleared when the data is retrieved

Example:

// when the form is processed successfully;

session_start();

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

// the redirect;

in the thank you page you can access the variable

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

now you can use the message variable to see the message


If I understand the question correctly, they are probably using ajax


Use a class and use it globally. ALso, post forms to empty (action="") so that way you can validate and save/send information all before determining what meessage to send, then give it to the Notifier class.

Then on every page, within the document have the print code.

eg.

<?
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>

You can do all kinds of cool things by doing it this way: form validation, sticky input, red-labled fields on erroneous inputs, form hiding on submission, and notification of everything. Here is an example of the Notification class:

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

I have created a revised version. It uses sessions and is a static class. Work great:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号