开发者

Multistep form in PHP - MVC App

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


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.


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.


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; ?>" />


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.


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.


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;
    ....
}


There are one great approach which will make your this task very easy without storing values in the session.

You just divide your multi step from in the different div and make all the div initially hidden except the first one. This way you can go back and next using jquery without loosing your data.

For example your form will like this

<form action="your_url" action="post"> 
    <div class="div1">
       your first page
    </div>
    <div class="div2" style="display:none;">
       your second page
    </div>
 </form>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜