4

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?

4
  • 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 Mar 6 '17 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 Mar 6 '17 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.html – Cerad Mar 6 '17 at 15:14
  • 1
    Gave the wrong link for fome submissions: symfony.com/doc/current/forms.html#handling-form-submissions – Cerad Mar 6 '17 at 15:20
2

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.

2
  • Wow, that was fast! You're welcome :) – DisgruntledGoat Mar 11 '17 at 14:51
  • Yes #PushNotification :D And that given me an idea to do it very smootly and easy ^^ – Nurrl Mar 11 '17 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.