Message after redirect

Ask about a PHP problem here.
Post Reply
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Message after redirect

Post 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?
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Message after redirect

Post 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]
<?php while(!$succeed = try()); ?>
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Re: Message after redirect

Post by FrederickGeek8 »

Thanks! That would work but is there some other way so the user doesn't see anything "different" about the url?
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Message after redirect

Post 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]
<?php while(!$succeed = try()); ?>
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Re: Message after redirect

Post 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]
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Message after redirect

Post 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]
<?php while(!$succeed = try()); ?>
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Message after redirect

Post 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.
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Re: Message after redirect

Post 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.
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Message after redirect

Post by Helx »

FrederickGeek8 wrote:if an error arose, it would still output that it was successfully updated.


Don't redirect if its not successful?
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Re: Message after redirect

Post 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.
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Message after redirect

Post 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
<?php while(!$succeed = try()); ?>
User avatar
FrederickGeek8
Posts: 148
Joined: Wed Nov 30, 2011 10:31 pm

Re: Message after redirect

Post by FrederickGeek8 »

... i never though of that... silly me
Post Reply