Forum Moderators: coopster

Message Too Old, No Replies

Multi-step form logic - best practice?

         

mr_nabo

1:27 pm on Jul 10, 2009 (gmt 0)

10+ Year Member



Hi,

I'm trying to build a form that has multiple steps in it i.e.

Step 1 (step1.php): Choose A, B or C.

If A, form action="step2.php"
If B, form action="step3.php"
If C, form action="step4.php"

And so on, where the choice of the user affects the form action.

I know Javascript is the wrong way to do this because of accessibility issues, so how can I do this in PHP? Is there a best practice for this kind of thing?

Any pointers very welcome.

Thanks

mn

janharders

1:47 pm on Jul 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd probably run it through one php-script and include the logic for the different styles if it's too long to keep the main script readable.
If users have to insert data in multiple steps, make sure they can go back to correct information without losing the input they made in the following forms.

rocknbil

5:17 pm on Jul 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a best practice for this kind of thing?

Avoid it if you can. :-) By that I mean, instead of multiple pages, you return to the same form with new values based on what's chosen. Take, for example, selecting a car:

1. Choose Make:

<select name="make" id="make">
<option value="Ford">Ford</option>
<option value="Chevy">Chevy</option>
<option value="Dodge">Dodge</option>
</select>

When submitted, if make is present, you output the model select list next to it; if make and model are present, output year; if all three are present, do the final action (search, in this case).


if (make) {
if (model && year) { //perform search }
else if (model) { // output make, model, year lists }
}
else { // output select make }

Of course, as mentioned, if any of the previous values change, you'll regenerate new sub-lists.

If you want multiple pages, you'll have to store previous iterations in hidden fields, session values, or cookies, but it would be nowhere near as flexible as the first solution.

mr_nabo

7:20 pm on Jul 10, 2009 (gmt 0)

10+ Year Member



Thanks for your replies, I'll try the single page PHP_SELF method and see if I can get anywhere and come grovelling back for help if I get stuck.

I can see the flexibility of having it all in one place - could I store the user's input in a session variable and then at the end of all the forms, or maybe even after each submission, store the info in a MySQL database?

Thanks

rocknbil

9:06 pm on Jul 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



or maybe even after each submission, store the info in a MySQL database?

You could, but will need a field for "completed" or something so you can separate abandoned records from completed ones.