Reputation: 947
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
Upvotes: 1
Views: 2192
Reputation: 151
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>
Upvotes: 0
Reputation: 546273
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:
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.
Upvotes: 2
Reputation: 2521
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;
I would not recommend saving using parameters, too many of them, you have to consider URL max size and browser limits.
Upvotes: 1
Reputation: 19251
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;
....
}
Upvotes: 0
Reputation: 9058
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.
Upvotes: 0
Reputation: 6581
A couple of relatively easy solutions:
<input type="hidden">
elements, so your $_POST
global will contain every answer given so farI 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.
Upvotes: 1
Reputation: 20601
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; ?>" />
Upvotes: 1