Controlling PHP WebApp State with SESSION
I'm creating a form wizard that requires the user to step through and complete each step before moving on to the next.
Right now, once a step is validated and completed, I switch $_SESSION['step']
to the next step in line, and check that variable when the next step loads to make sure it is valid. If a user tries to skip to a step that is out of sequence, the $_SESSION['step']
prevents the step from loading.开发者_C百科
Is this the proper approach? Also, once a step is completed, I want the user to be able to go back and edit the values that were entered on any given previous step. My approach doesn't allow for this, as I'm not sure how I'd set change the session to the step they are revisiting without knowing if it is a valid request or not.
Any input on this would be much appreciated!
There's nothing wrong with this approach, although you might be a bit limited in that this doesn't work for more than one form only. However, if this is just one small app, then no worries.
The second question I'm not sure I understand, but $_SESSION can accept any data type, including arrays, so just fill a $_SESSION with where you are and what you've done in the form so far ;
$_SESSION['my_form'] = array (
'current_step' = 'step_2',
'step_1' = array ( // the form elements ),
'step_2' = array ( // the form elements ),
'step_3' = array ( // the form elements ),
'step_4' = array ( // the form elements ),
) ;
Next, you provide validation at each point as normal. You can also put a little flag into each form whether they have been filled out or not ;
'step_2' = array (
'complete' = true,
// form elements / fields here
),
Another way to slice this is to use one of the many thousands of dynamic forms. For example, using JQuery, you can use the Accordion plugin (from JQuery UI) to simulate the whole thing in just one HTML page.
精彩评论