Jump to content

A good way to display messages after postback


ecce

Recommended Posts

I'm looking for a nice, simple way to handle postbacks and specifically messages to the user when the postback work is done. This is my main three-step "procedure" at the moment:

 

- Form send postback data

- postback is handled before any output at the beginning of the sites scripts.

- When done, header() forwards the user to the same URL, so the user ends up on the same page, but without the warning of sending post data again if the user refreshes the page.

 

Now, I want to show a message box, like "User data saved" or "An error occured". Any suggestions on how to get the success/error messages to the user (preferably without using GET variables like file.php?success=1)?

 

Link to comment
Share on other sites

You have to send some kind of trigger for it to recognize a success. And basically your options are GET variables, SESSIONS, redirecting them to a different script, or javascript/ajax. Why are you against using the "success=1" option?

It's ugly. :) And it gets quite messy when you want to display a range of messages. mod_rewrite may also prevent GET from being a suitable solution.

 

I've tried setting message info in $_SESSION. When the page is displayed, the $_SESSION is checked and if not empty the message is displayed, and the deleted from $_SESSION. However, this resulted in a very wierd behaviour. The $_SESSION flag is not deleted until the page is loaded for the second time (???). Result: the message is showed twice. I guess the zend engine tries to be clever and does some kind of out-of-order execution or something...

Link to comment
Share on other sites

I have never had a problem with mod_rewrite preventing GET from being a suitable solution for what you are doing. The other solution is to use the db and set up a table that handles, like the user_id and some flag that says they need a message. But that seems a little over the top for something that should be very simple.

Link to comment
Share on other sites

I always use $_SESSION variables, something like this will work.
 

<?PHP
  session_start();

  if(isset($_SESSION['message'])) {
    echo $_SESSION['message'];
    unset($_SESSION['message']);
  }
?>
Edited by PaulRyan
Link to comment
Share on other sites

 

I always use $_SESSION variables, something like this will work.

 

<?PHP
  session_start();

  if(isset($_SESSION['message'])) {
    echo $_SESSION['message'];
    unset($_SESSION['message']);
  }
?>

That's exactly how I did it, and every time the message popped up twice. But I'll have another look at it I think... it feels better that using URIs to send data to client.

Link to comment
Share on other sites

After you have set the session variable, make sure you re-direct to the page AND exit after the re-direct?

 

 

<?PHP
  if (blah == blah) {
    $_SESSION['message'] = 'Blahhhhh';
    header('Location: blah.php');
    exit;
  }
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.