I use a class to interact with $ _POST, similar to the following:
// create the object $post = new PostData(); // register your requirements... write whatever methods you need // for each call,read $_POST, check criteria, and mark the field // as good or bad... $post->required ('LastName'); $post->required ('FirstName'); $post->numeric ('Age'); $post->optional ('MiddleInitial'); $post->regExp ('/\d{3}/','AreaCode'); $post->email ('Email'); // check the status if (!$post->isValid ()) { $_SESSION['FailedPostData'] = $post; header ('Location: page.php'); } // normal form processing
Then on the page. php you can see if there is a FailedPostData in the session, read it to find the information entered last time, as well as which fields failed. I use a macro template engine that allows me to easily re-populate form inputs and flag failures. Otherwise, you can get a lot of code for a simple form ...
You will also need a mechanism to ensure that deprecated FailedPostData do not hang in the session and confuse things.
source share