February 4, 200916 yr Hello, When submitting a form to another page or itself and an error is returned (either by FM or verification code), is there an easy way to keep what the user had filled in in-tact when reloading the form page to display the error/message..besides using sessions? Thanks for your help!
February 5, 200916 yr Yes and No. PHP maintains several predefined variables. Three which are relevant here are: $_POST, $_GET, and $_REQUEST. ($_REQUEST contains $_GET + $_POST + $_COOKIE); They are associative arrays using the name attribute from the elements on the submitted form as keys to the elements' values. You can use these to repopulate your form on a submit error. A simple trick is to have the form submit to the same page. If the submission is successful then show the successful submission HTML; if the submission fails show the form. The one caveat is that PHP does use sessions to maintain these variables. It manages the sessions so there is no need to worry about calling session_start(), but it does require cookies so that it can grab the session id from the client.
February 5, 200916 yr Author Thanks for the reply. That makes sense.... If I understand correctly, I would preload the form fields with $_REQUEST['formfieldname'] - which wouldn't show on initial load but would once they submit, POSTing those variables, and reload the form with an error and submitted information. Does that sound about right? Or would that throw an error on the initial load?
February 5, 200916 yr That will throw a warning; do this: <? $field1 = isset($_REQUEST['field1'])?$_REQUEST['field1']:""; ?> <? Side note to Mods: the code tags are still stripping ?> from code this makes them somewhat problematic for displaying code that switches between php and html
Create an account or sign in to comment