views:

638

answers:

6

Hello,

I'm about to program a mutistep form in my MVC App (Self-made MVC framework) in which no data is to be inserted until the last step is finished.

I got the following issues/questions

  • I'd really like to find a kind of pattern to solve this problem, is there any?
  • I want it to be easy the perform a go back action, and get the data back in the last step. Question: Is a session the only way to pass the data? Are get parameter maybe a better/simpler option?
  • How have you solved this problem before?

Regards, Guillermo

+1  A: 

I wouldn't go with data in anything but cookies/sessions, at least not if you want users to be able to go back and forth between your steps.

In your controller:

$_SESSION['yourform']['optionalSubformOrPage']['field'] = $_POST['field'];
header('Location: nextpage');
die;

In your view:

<input name="field" value="<?php echo isset($_SESSION['yourform']['optionalSubformOrPage']['field']) ? $_SESSION['yourform']['optionalSubformOrPage']['field'] : null; ?>" />
chelmertz
+1  A: 

A couple of relatively easy solutions:

  • Save the data into a second DB table, and only copy it to the primary table at the end
  • Save your data in <input type="hidden"> elements, so your $_POST global will contain every answer given so far
  • Save the data to a session / cookie

I wouldn't ever use GET params for a task like this; it's too easy to leave out one or two, and most browsers have a relatively short maximum URL length. Either cookies or hidden <input>s will be your best bet in this case.

Duroth
A: 

I've done this myself. I started out trying to avoid a dependency on the session by storing values from other steps in hidden form fields but this quickly became unwieldy so in the end I resorted to using the session.

Andy
A: 

You can also pass variables with hidden fields but with tools like firebug those fields can be modified and it's not a good idea. I think that session is the best way to store this type of data.

Anyway, in php code, i always store a variable that indicates which form must be shown:

switch($_SESSION['step'])
{
    case 1: echo "<form name='first'...";
    break;
    case 2: echo "<form name='second'...";
    break;
    ....
}
mck89
A: 

Look how they implemented it in Symfony, it's quite well built in that framework.

You just have to download it and look in your project: /lib/symfony/form

Basically you should create classes for each one of your form inputs;

  • you could always create different and complex ones, then;
  • you go create validator classes for each of them (maybe configurable),
  • you store your data in PHP objects that you save in the session or cookie,

I would not recommend saving using parameters, too many of them, you have to consider URL max size and browser limits.

Lex
+1  A: 

Duroth has given some good suggestions. Another option is to store the data into the database as you go, step by step, but keep a flag on that data to show that it is incomplete. When they submit the last page of the form, then insert the data and mark it as complete. The problems you'll run into with this method are:

  • Incomplete data being left in the table
    • You'll need to run some maintenance to clear out old incomplete data (older than a week, or something)
  • It might make the rest of your program logic much more complicated. You'd have to always check that the record is "complete".

If you were to store the incomplete data in another table, like Duroth mentioned, then you'd avoid the second issue, but you'd have some duplicated tables in your database.

nickf