I have a controller which displays and processes postback for a form. The controller redirects to another page once the form has been successfully validated and the data has been processed accordingly.
Redirection is completed with something to the equivalent of:
@header('Status: 303 See other');
@header('Location: ' . $redirect_uri);
What is the most reliable way for the controller to provide a message that is to be displayed on the new page? Is there a standard practice for this?
i.e. "Your account was successfully updated."
My thought is to place dynamically generated messages into a session variable, and to indicate a predefined message with a unique ID in the URI itself:
$_SESSION['previous-response-msg'] = "Account of '$user_name' was updated.";
or
$redirect_uri = 'http://example.com/other-page?msg=42';
One time message in session variable has worked fine for me. I don't see much of a problem here, especially for status type messages.
If you are using a framework, its quite possible this is built in. .
Using session will allow more flexible messages, but either way will work. There's no 'best practice' that I'm aware of.
Your second method is a great way to adjust non-secure page content. Wherever you'd like the message to appear on your page you could...
if (isset($_GET['msg']) && $_GET['msg'] == '42') {
echo '<div>Your account was successfully updated</div>';
}
Simple enought, I think.
Could you not use ajax and then add a success and failure callback which displays the response data from the file you sent data too ?