CodeIgniter Forums

Full Version: How to exchange user data with the same class instance
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Team,

I am using a controller which has several functions to process multiple steps in a user-process posting data to our server.

So we are building up a complete page by letting the user incrementally add information for each step.

I would like to store the information in public class variables. so they are available to all the functions when they render their respective views.

However, I cannot see a way to code the "action=" html form attribute so that I reenter the running instance of the controller (if indeed this is possble?). I have tried action="<?php $this->controller_function() ?>" and variants of this but this produces PHP FATAL error because it has no object context.

Currently, the code uses a URL similar to controller/function but this instantiates a new instance thereby losing the class variables previously set. $_SESSION is also used to keep values across instantiations.

I was really trying to tidy things up - so perhaps my question should be is there a better pattern for managing such a process?

Apologies if this isn't clear enough, there is a distinct possibility I have fundamentally not understood something :-(

regards, Paul
If I understand you right, you want your form to submit to the current url?
Just omit the action attribute. If you use the form helper (or the form_validation library), you can start a form like this (in your view):
PHP Code:
<?= form_open();?>
When I do a multi step form process like you are describing (and I presume you are not using ajax) I change the url of the form action depending on the step like:

PHP Code:
<?php echo form_open('the_controller/the_method/'.$step); ?>
Where the step is step_1 or step_2 etc.

The controller then looks at this value, if not set it issues step_1, if set it does form_validation for step_1, which if passed, then sets the step to step_2 etc.

I store the values of the various steps usually in the session. I also for step_2, say, check that the session info is set for step_1, so I do not just trust the url in the form (as a user might have changed it manually). If on step_4 the session variables are not set for say step_2, I would show step_2.

If it is a very big form, or a form that needs to be persistent, like billing address and delivery address, I store the info in a database table linked to the user. If the user half fills out the form, then leaves, then logs in again, going to the form automatically loads the info already set for the step.

This works particularly well for checkouts.

For a one page checkout and ajax I put the step in a hidden field. Then the ajax return either returns error fields or the new form body for the next step. This is nice but is a bit more complex than doing page loads.

The only alternative I know of is javascript multi step forms, where the next button hides the current step and shows the next step, or reveals the next tab, but in affect it is just one giant form, just with some display trickery. I am not keen on this as options set in step_2 for instance can mean step_3 is missed out, or info or further options change depending on the previous choices. This all gets complicated in a single form solution.

So to answer your question, I think a step indicator (in url or hidden field) with data stored in session until step process is completed or confirmed, is the best solution.

Coded cleanly and carefully, it can also be quite clear what is happening and easy to debug too.

Hope that helps,

The web works with requests and responses.

You clicking a link, submitting the form, etc. is the request; CI with everything in it running is the response. Each time, the response is an entirely new process. You can't loopback to the "same instance" because it no longer exists.
Hi Woutrr60 not quite the current solution posts the data for the step to a different function in the controller. The design is effectively a function for rendering the form and the one for storing the new data. One further question if I may? The start process displays the first view and in the process instantistes the controller class instance ( I think). If I then post the form to another function within that controller am I right in assuming this is a different instance or does it magically find the original instance? In the latter case I would expect the class variables to still be set? As I said I’m wallowing a bit in the detail here alas so any enlightenment wouy really be appreciated, regards Paull
Ok just seen other comments thx very much I guess our original design is pretty much what you described pauld thx again for you patience!