ecce Posted May 3, 2013 Share Posted May 3, 2013 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)? Quote Link to comment https://forums.phpfreaks.com/topic/277600-a-good-way-to-display-messages-after-postback/ Share on other sites More sharing options...
akphidelt2007 Posted May 3, 2013 Share Posted May 3, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/277600-a-good-way-to-display-messages-after-postback/#findComment-1428006 Share on other sites More sharing options...
ecce Posted May 3, 2013 Author Share Posted May 3, 2013 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... Quote Link to comment https://forums.phpfreaks.com/topic/277600-a-good-way-to-display-messages-after-postback/#findComment-1428016 Share on other sites More sharing options...
akphidelt2007 Posted May 3, 2013 Share Posted May 3, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277600-a-good-way-to-display-messages-after-postback/#findComment-1428019 Share on other sites More sharing options...
PaulRyan Posted May 3, 2013 Share Posted May 3, 2013 (edited) I always use $_SESSION variables, something like this will work. <?PHP session_start(); if(isset($_SESSION['message'])) { echo $_SESSION['message']; unset($_SESSION['message']); } ?> Edited May 3, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/277600-a-good-way-to-display-messages-after-postback/#findComment-1428043 Share on other sites More sharing options...
ecce Posted May 4, 2013 Author Share Posted May 4, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277600-a-good-way-to-display-messages-after-postback/#findComment-1428172 Share on other sites More sharing options...
PaulRyan Posted May 4, 2013 Share Posted May 4, 2013 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/277600-a-good-way-to-display-messages-after-postback/#findComment-1428244 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.