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?

gnat
20.5k29 gold badges117 silver badges308 bronze badges
asked Mar 6, 2017 at 9:47
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. Commented 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. Commented 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.html Commented Mar 6, 2017 at 15:14
  • 1
    Gave the wrong link for fome submissions: symfony.com/doc/current/forms.html#handling-form-submissions Commented Mar 6, 2017 at 15:20

1 Answer 1

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.

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.