I know that you aren't supposed to output anything to the browser before using the header location function but what if you need to show an error message? If there is an error, for example, an invalid email address, then I want to show an error message and go back to the form submission page. After all of the errors have been corrected, I want to redirect to the next page. However since I already sent an error message, I can't send a header. Is there a way to do this without resorting to javascript?
Page Redirect after Error Message is Output to the Browser
Started by makamo66, May 19 2010 03:28 PM
9 replies to this topic
#1
Posted 19 May 2010 - 03:28 PM
|
|
|
#2
Posted 19 May 2010 - 03:50 PM
When doing this, I recommend that you have both things on the one page.
For example..
<CODE FOR AFTER FORM SUBMISSION (Validations, gets, posts, inserts, updates and all)>
Set the error variable to say if there is an error or not.
Also set the error message for that field.
If !(error) {header("location: index.html");} // <-- or another location altogether.
otherwise it continues.
<CODE FOR THE FORM ITSELF>
echo "<form action='".$Server['PHP_SELF']."' Method='POST'>"
Next to the field in the form, set a text spot for error messages.
</form>
Well, you get the idea.
This is the easiest way to accomplish your task. Also the more reliable way, because then no passing of variables around everywhere.
For example..
<CODE FOR AFTER FORM SUBMISSION (Validations, gets, posts, inserts, updates and all)>
Set the error variable to say if there is an error or not.
Also set the error message for that field.
If !(error) {header("location: index.html");} // <-- or another location altogether.
otherwise it continues.
<CODE FOR THE FORM ITSELF>
echo "<form action='".$Server['PHP_SELF']."' Method='POST'>"
Next to the field in the form, set a text spot for error messages.
</form>
Well, you get the idea.
This is the easiest way to accomplish your task. Also the more reliable way, because then no passing of variables around everywhere.
#3
Posted 19 May 2010 - 09:46 PM
Hmm, I actually think Headers are best to be used for direct page 'redirect'. For redirecting in, say, 5 seconds while showing a message, meta refreshes could be used best I suppose.
Ex.
In this example it will show the message 'Error - error here' for 5 seconds and then redirect the page to 'redirect_to_page.php'.
Ex.
<META HTTP-EQUIV="refresh" CONTENT="5; url=redirect_to_page.php"> Error - error here
In this example it will show the message 'Error - error here' for 5 seconds and then redirect the page to 'redirect_to_page.php'.
#4
Posted 20 May 2010 - 05:34 AM
Well, that works for dumb people or people that don't know much about some things. HOWEVER, I have that turned off, and I do not allow those redirections to occurr, and nor do many many people in this world.
HOWEVER, what you have there would not always work anyway, because sometimes it takes MORE than 5 seconds for a page to load and they would completely miss the message. So that is the FIRST flaw in your redirect. I know people will try to prove me wrong, but I know what I know and have had it happen recently when using IE as my browser, the page didn't load and I was redirected after 20 seconds.
It would be best to do it the way I recommend, because then IF it's successful then you could EASILY just load the page you want into the page INSTEAD of the information page.
For example...
Not only is this an easier way of doing things, but you can also have the variables on the pages you are going to so that you can display a completed message as a choice, and have it as the home page. As for the Error, you merely echo that onto the form page again.
It really isn't that hard to do it with PHP. It's so simple and easy, and means that smart people don't get frustrated and complain. NOR will you have people try to hack your site and cause it to be blacklisted and then be caused to be a "spyware site", which has happened to people that I know.
I know PHP/MySQL, and I'm not the best, but I know what people do and do not allow because I work in the internet industry, I write websites for a living, and I have NEVER used an HTML redirect like that. They are pointless because of what people do to protect themselves from them.
But, not everyone is protected. So there are many people who would allow these "redirects" to occurr. Which can be bad, or good, or hacked into other code.
I am only offering the easiest way to achieve the goal that will be doable for ANYONE who visits the site. (IF the server has PHP).
HOWEVER, what you have there would not always work anyway, because sometimes it takes MORE than 5 seconds for a page to load and they would completely miss the message. So that is the FIRST flaw in your redirect. I know people will try to prove me wrong, but I know what I know and have had it happen recently when using IE as my browser, the page didn't load and I was redirected after 20 seconds.
It would be best to do it the way I recommend, because then IF it's successful then you could EASILY just load the page you want into the page INSTEAD of the information page.
For example...
<?php
// Do field check. Then put error.
$error = "";
$errorscount = count($errors);
$lcv = 0;
do
{
$error = $error ."<br>". $errors[$lcv];
$lcv++;
}
while ($lcv < $errorscount);
If !(error)
{
INCLUDE_ONCE("completepage.php");
}
else
{
INCLUDE_ONCE("formpage.php");
}
?>
Not only is this an easier way of doing things, but you can also have the variables on the pages you are going to so that you can display a completed message as a choice, and have it as the home page. As for the Error, you merely echo that onto the form page again.
It really isn't that hard to do it with PHP. It's so simple and easy, and means that smart people don't get frustrated and complain. NOR will you have people try to hack your site and cause it to be blacklisted and then be caused to be a "spyware site", which has happened to people that I know.
I know PHP/MySQL, and I'm not the best, but I know what people do and do not allow because I work in the internet industry, I write websites for a living, and I have NEVER used an HTML redirect like that. They are pointless because of what people do to protect themselves from them.
But, not everyone is protected. So there are many people who would allow these "redirects" to occurr. Which can be bad, or good, or hacked into other code.
I am only offering the easiest way to achieve the goal that will be doable for ANYONE who visits the site. (IF the server has PHP).
#5
Posted 20 May 2010 - 07:36 AM
AndyW said:
Well, that works for dumb people or people that don't know much about some things.
I'm not quite sure what you are uggesting here?
And I really don't see the danger of using meta refreshes anyway. Also I don't see how your code would work? I think you should give an example because the code you provided seems ( as far as I can see ) to only add errors into a variable $error and includes a target page based on that. I mean, I see how it would works if you'd just want to either show one page or another but I don't see how that REDIRECTS a user or shows the error for a certain time before redirecting. As the code you provided just either shows an error or shows a form, don't see any redirections ( nor time intervals ).
#6
Posted 20 May 2010 - 03:32 PM
I'm not suggesting anything, but your long statement has suggested it, since you obviously didn't read the code.
The errors are put into an array for use later, depending on what errors are found.
If there is an error then it displays the form again and says what errors they are on the form. (Logically enough).
If there are no errors then it shows the complete page, where they go if they have entered the form correctly and the form is processed.
As for redirecting after a certain amount of time, there is no safe way to do this that will work with everyone. If you aren't going to think of EVERYONE that will visit the website, then you would be no better than people that build website to ONLY work with Internet Explorer.
As for the re-direction, that does NOT NEED to be done essentially, because the user will see a different page if things are right, and be returned to the form if it's all working.
They will perciece a redirection. For they will be on a different page.
Take my comments however you want, they are not a stab at you or anyone directly, they are mere pointing things out to people. I merely am offering the BEST advice since this is a PHP section of the forum, so they are requesting PHP, NOT HTML.
If you can't see the difference between these 2 coding types then you either can't read or have a bigger problem. (yes, this is a stab at you, please prove me wrong)
If they REALLy want a complete re-direct without passing variables, then my method is easiest, because you have to THINK of the END USER. If you go back to the form and they see there is no problem there, some people are likely to submit it again, or some will correct the errors they remember and forget a few and submit again. The way I am recommending as the easiest way will remove any possible flaw in the functions for the end user.
If they REALY want a re-direct, then they can replace
Other advantages to this PHP way is so that if the form is complete, it can change the page, but not have to reload, thus be faster, because it will have already loaded the first part, and performing another request to the apache server seems like a pointless exercise since you would probably have already opened the database by loading the site, this means you can finish using the connection and just have information on the complete page, and thus not worry about taking up precious resources by closing the DB connection, requesting another page, opening the connection and using it again.
there are MANY advantages to the way that I recommend compared to the META refers that is obsolete and doesn't always work.
So it's all up to Mako how they want to do it, my easy and PROVEN EFFECTIVE way, or the way that you recommend that is out dated and out of style. It's all personal opinion, but that's just the way the world turns these days. One person has one opinion, another has another opinion, and it can cause quarrels or tiffs, even just discussions as to why things are aparently better one way than the other.
I have stated why I know this way to be better, now if you want, you can say why your meta redirect is better.
The errors are put into an array for use later, depending on what errors are found.
If there is an error then it displays the form again and says what errors they are on the form. (Logically enough).
If there are no errors then it shows the complete page, where they go if they have entered the form correctly and the form is processed.
As for redirecting after a certain amount of time, there is no safe way to do this that will work with everyone. If you aren't going to think of EVERYONE that will visit the website, then you would be no better than people that build website to ONLY work with Internet Explorer.
As for the re-direction, that does NOT NEED to be done essentially, because the user will see a different page if things are right, and be returned to the form if it's all working.
They will perciece a redirection. For they will be on a different page.
Take my comments however you want, they are not a stab at you or anyone directly, they are mere pointing things out to people. I merely am offering the BEST advice since this is a PHP section of the forum, so they are requesting PHP, NOT HTML.
If you can't see the difference between these 2 coding types then you either can't read or have a bigger problem. (yes, this is a stab at you, please prove me wrong)
If they REALLy want a complete re-direct without passing variables, then my method is easiest, because you have to THINK of the END USER. If you go back to the form and they see there is no problem there, some people are likely to submit it again, or some will correct the errors they remember and forget a few and submit again. The way I am recommending as the easiest way will remove any possible flaw in the functions for the end user.
If they REALY want a re-direct, then they can replace
INCLUDE_ONCE("completepage.php"); with header('Location: ./completepage.php');
It's not that hard. Even they said this, and they didn't want to resort to javascript, and a META Redirect is a useless exercise these days. Since you can even set a timeout with PHP.sleep(5); //Causes the script to pause for 5 seconds before continuing to execute.However this is useless to Maka because it's only script execution, and NOT display of data and the like.
Other advantages to this PHP way is so that if the form is complete, it can change the page, but not have to reload, thus be faster, because it will have already loaded the first part, and performing another request to the apache server seems like a pointless exercise since you would probably have already opened the database by loading the site, this means you can finish using the connection and just have information on the complete page, and thus not worry about taking up precious resources by closing the DB connection, requesting another page, opening the connection and using it again.
there are MANY advantages to the way that I recommend compared to the META refers that is obsolete and doesn't always work.
So it's all up to Mako how they want to do it, my easy and PROVEN EFFECTIVE way, or the way that you recommend that is out dated and out of style. It's all personal opinion, but that's just the way the world turns these days. One person has one opinion, another has another opinion, and it can cause quarrels or tiffs, even just discussions as to why things are aparently better one way than the other.
I have stated why I know this way to be better, now if you want, you can say why your meta redirect is better.
Edited by Drew, 20 May 2010 - 03:35 PM.
Spelling error.
#7
Posted 20 May 2010 - 09:51 PM
Actually I was talking about the redirection interval which I really can't find in your code.
So the reason I think meta redirect should be used as I still don't see how using headers/includes could do this including the timeintervals that can be set using meta redirects. And now you say using sleep could be done but I also see that's kind of useless here as it will just wait with including a file. So I still don't see how it could, for example, do this: show an error message for 5 seconds, then redirect to next page.
Like many systems have such a redirect system.
So the reason I think meta redirect should be used as I still don't see how using headers/includes could do this including the timeintervals that can be set using meta redirects. And now you say using sleep could be done but I also see that's kind of useless here as it will just wait with including a file. So I still don't see how it could, for example, do this: show an error message for 5 seconds, then redirect to next page.
Like many systems have such a redirect system.
#8
Posted 20 May 2010 - 10:37 PM
Obviously YOU haven't read what I've said. So I'm not even going to try to explain it to YOU when YOU won't read what I have said.
Many systems I have seen have NOT used that functionality. I'm done with you and your nonsensicle way that doesn't listen.
If you want me to explain further it'll cost you.
Good day WebCodez.
mamako, if you want me to explain it to you, I will, at no charge.
Many systems I have seen have NOT used that functionality. I'm done with you and your nonsensicle way that doesn't listen.
If you want me to explain further it'll cost you.
Good day WebCodez.
mamako, if you want me to explain it to you, I will, at no charge.
#9
Posted 21 May 2010 - 01:00 AM
Please stop this flaming now, both.
There are many ways of solving this issue, where everything ends up in how you really want the finished page to look and work.
Meta-redirects is one way. Yes, it is not something each and everyone has in their browsers, as some people turn it off or has browsers not supporting it. but the big public has it. Many recommendations I've read is to avoid it, but as it works for most people, people will use it, and they who want to, do it on your own risk.
But When I read the original post, it doesn't say that it's a time interval between the errors and the new form. It can also be a layouted interval on the page.
I think that the easiest way to achieve a layouted interval is to let the form page submit to itself, present the errors inside the form (at each control) or at top of the form.
There are many ways of solving this issue, where everything ends up in how you really want the finished page to look and work.
Meta-redirects is one way. Yes, it is not something each and everyone has in their browsers, as some people turn it off or has browsers not supporting it. but the big public has it. Many recommendations I've read is to avoid it, but as it works for most people, people will use it, and they who want to, do it on your own risk.
But When I read the original post, it doesn't say that it's a time interval between the errors and the new form. It can also be a layouted interval on the page.
I think that the easiest way to achieve a layouted interval is to let the form page submit to itself, present the errors inside the form (at each control) or at top of the form.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#10
Posted 21 May 2010 - 03:08 AM
Let me add one more thing to this conversation, @andyW - there obviously ARE many famous systems using redirection by timeinterval such as forum systems: vBulletin, IPB, pretty much any of them ( in combination with a link given in case it isn't working for say, very few people ). However I don't say they must have used meta, but I could not see how the code you provided would do this in any way, at all.
Beside that I also think this discussion won't lead to anything good as we appear to, lets say, have another view on things.
Also I don't appreciate it when people talk to me with such disrespect while Im just telling my oppinion and trying to understand yours, so I wouldn't even want to continue this discussion with you, 'AndyW'.
btw, I assumed the author meant to have a time interval as he stated that he'd like to show an error message and then go back to form page. So my bad if I interpetated that the wrong way. As for just showing errors I agree to the solution of Orjan.
Beside that I also think this discussion won't lead to anything good as we appear to, lets say, have another view on things.
Also I don't appreciate it when people talk to me with such disrespect while Im just telling my oppinion and trying to understand yours, so I wouldn't even want to continue this discussion with you, 'AndyW'.
btw, I assumed the author meant to have a time interval as he stated that he'd like to show an error message and then go back to form page. So my bad if I interpetated that the wrong way. As for just showing errors I agree to the solution of Orjan.


Sign In
Create Account


Back to top









