I had some ideas, but which one is the best (and why), and is it other way(s) to do this?
I thought about :
- Sessions
- GET Params, but if there is a lot of errors(for example a register form) it could be ugly.
- POST Params, but it needs a form with a JS redirection.
Objectively what is the best solution?
-
What type of error, validation errors or application errors? Why are you passing between pages? Objectivity there may be no best solution, it is very application and framework dependant.Jeremy French– Jeremy French03/06/2017 09:57:42Commented Mar 6, 2017 at 9:57
-
It's to pass validation error, the workflow is this one : page.php(form) -> validate.php (if error : Redirect back with errors; else : redirect to another page;). PS : I did it from scratch.Nurrl– Nurrl03/06/2017 10:00:41Commented Mar 6, 2017 at 10:00
-
There are very few objectively "best" solutions in programming. However, if you eliminate the redirect following an error then you eliminate the need to store the error. This is the approach the Symfony framework takes: symfony.com/doc/current/components/dependency_injection.htmlCerad– Cerad03/06/2017 15:14:29Commented Mar 6, 2017 at 15:14
-
1Gave the wrong link for fome submissions: symfony.com/doc/current/forms.html#handling-form-submissionsCerad– Cerad03/06/2017 15:20:14Commented Mar 6, 2017 at 15:20
1 Answer 1
The de facto way to do this in PHP is using something called flash data, which uses sessions. In your validate.php
you set the error message(s) in a session variable, then on your page.php
you read from the session variable and delete it from the session (so that it doesn't keep showing up on that form).
This is used in most frameworks, for example Laravel and CodeIgniter.
Here's a Stack Overflow answer that shows some basic code in PHP. Or there are standalone libraries to help you such as PHP Flash Messages.
-
Wow, that was fast! You're welcome :)DisgruntledGoat– DisgruntledGoat03/11/2017 14:51:10Commented Mar 11, 2017 at 14:51
-
Yes #PushNotification :D And that given me an idea to do it very smootly and easy ^^Nurrl– Nurrl03/11/2017 14:52:44Commented Mar 11, 2017 at 14:52