I have a page with a form on it.
If the forms is submitted I redirect to that page again.
On the page I want to display a notification that the data is saved.
I'm looking for a clean way to know that I have to display the notification.
First I thought of adding something to the url:
/page?notify=1
开发者_开发百科
But I don't like to change the URL.
Then I thought of setting a session before the redirect.
And on the page I check if the session is set, display the notification if it is and unset it.
Would that be an acceptable way of doing it or is there perhaps a better / cleaner way?
I personally think the ?notify=
is perfectly acceptable. You're not going to have spiders crawling that page and since it's only a confirmation notification you don't need to worry about the page being indexed. Don't be afraid of query strings when you're out of the SEO realm - just look at Amazon.com :)
Ultimately it comes down to code re-usability and effort of implementation.
A session is fine. A cookie that you delete immediately after would be even cleaner. Since a session id is usually a larger cookie than this anyways, and takes storage on your server.
setcookie('notify',1,time()+60); // Expire in one minute. Redirect should be faster.
Then on your page they are being redirected to:
if(isset($_COOKIE['notify']) && $_COOKIE['notify'] == 1){
// Notification output
setcookie('notify', '', time()-1);
}
Another Option you have is to is redirect the user to /page/notify and use your .htaccess with Rewrite Mod to rewrite that as /page?notify.
Something like:
RewriteRule ^(.*)/notify/?$ $1?notify=1
Since you have a form, maybe you also have a submit button. You can check if the form is submitted like this:
<?php
if(isset($_POST['go'])) {
// The form has been submitted.
echo("Saved!");
}
?>
<form method = 'post' action = ''>
<input type = 'submit' id = 'go' name = 'go' value = 'Submit'/>
</form>
EDIT
Since that won't work.. I generally keep a session variable as an array (I call it 'errors', but I use it for just general output). It's an array because it can hold multiple messages, and then on every single page display I dump whatever is in that array whererever I want to display those messages. This has worked for me across multiple projects, and seems to me to be a pretty good way of storing messages to be sent to the user
session_start();
$_SESSION['errors'] = array();
...
array_push($_SESSION['errors'], "<span style = 'color:green;'>Success!</span>");
header("Location: somepage.php");
And then display...
if(isset($_SESSION['errors']) && count($_SESSION['errors']) > 0) {
foreach($_SESSION['errors'] as $k => $v)
echo($v . "<br/>");
unset($_SESSION['errors']);
}
first_page.php
$_SESSION['notify'] = 9;
second_page.php
if($_SESSION['notify'] > 0){
notify(9);
$_SESSION['notify'] = 0;
}
You can add a hidden field in your form like this:
<input type="hidden" name="action" value="submit" />
Then when the page is reloaded you can do this:
if($_POST['action'] == "submit"){
// show that data was saved
}
I mean ideally you'll want to make sure the data was actually saved but Im not getting much into your implementation.
精彩评论