Page 1 of 1

Message after redirect

Posted: Mon Dec 24, 2012 3:32 am
by FrederickGeek8
When a user updates his/her settings I need to have it so after it runs the update function, it redirects back to itself, making sure that the information appears updated to the user. The problem is, is that I also want to display a message that displays something like "Updated!". How do I do this?

Re: Message after redirect

Posted: Mon Dec 24, 2012 3:36 am
by ExtremeGaming
Sounds like it would be a good idea to use the URL. Something like file.php?update=success

Then:

[syntax=php]<?php
if(isset($_GET['update']) && $_GET['update'] == "success") {
echo "Updated!";
}
?>[/syntax]

Re: Message after redirect

Posted: Mon Dec 24, 2012 4:01 am
by FrederickGeek8
Thanks! That would work but is there some other way so the user doesn't see anything "different" about the url?

Re: Message after redirect

Posted: Mon Dec 24, 2012 5:17 am
by ExtremeGaming
You can use a url rewrite in .htaccess I believe.

Try adding this to your .htaccess Replace the two occurrences of file with your file name, excluding the extension (assuming it ends in .php, if not replace .php with .whatever)

Code: Select all

RewriteEngine On
RewriteRule ^file\.php$ /file.php?update=success [L]

Re: Message after redirect

Posted: Mon Dec 24, 2012 6:01 am
by FrederickGeek8
OK a big problem with this is if it is updated, then the user tries to update another field, where they do something wrong and it throws an error like "Please enter a password" then the webpage reads
[syntax=text]Updated!
Error: Please enter a password
*form*[/syntax]

Re: Message after redirect

Posted: Mon Dec 24, 2012 6:49 am
by ExtremeGaming
It seems rewriting the URL would cause major issues with your whole updating. Major issues that would most likely take an extremely long time to work around. So, another option would probably be good using sessions. Wherever you are running the function, set a session like $_SESSION['update'] = "win"; Then back on your page:

[syntax=php]
<?php
session_start();

if(isset($_SESSION['update']) && $_SESSION['update'] == "win")) {
echo "Updated!";
// You want to remove the session so it doesn't have the (low) possibility of slowing down the site, and if the user returns to the page it won't show Updated! every time
session_unset($_SESSION['update']);
}

?>[/syntax]

Re: Message after redirect

Posted: Mon Dec 24, 2012 8:04 pm
by Helx
I'm sorry, but why do you want to hide any URL changes?
It's too much work to just use htaccess and sessions.

Just stick to GET variables in the URL. It easier to understand.

Re: Message after redirect

Posted: Tue Dec 25, 2012 2:36 am
by FrederickGeek8
Helx wrote:I'm sorry, but why do you want to hide any URL changes?
It's too much work to just use htaccess and sessions.

Just stick to GET variables in the URL. It easier to understand.

I'm fine with not hiding the url but using GET alone would make it so if an error arose, it would still output that it was successfully updated.

Re: Message after redirect

Posted: Tue Dec 25, 2012 3:51 am
by Helx
FrederickGeek8 wrote:if an error arose, it would still output that it was successfully updated.


Don't redirect if its not successful?

Re: Message after redirect

Posted: Thu Dec 27, 2012 5:15 am
by FrederickGeek8
Helx wrote:
FrederickGeek8 wrote:if an error arose, it would still output that it was successfully updated.


Don't redirect if its not successful?

If it is first updated successfully, then it reads "Update". If the user does not change pages, then the user changes something else and does not update successfully, then ?update is still present.

Re: Message after redirect

Posted: Thu Dec 27, 2012 6:22 am
by ExtremeGaming
Why not use the form action attribute to set the form to submit to the same page? It would automatically remove the ?update from the URL

Re: Message after redirect

Posted: Thu Dec 27, 2012 6:29 am
by FrederickGeek8
... i never though of that... silly me