Welcome Guest, Not a member yet? Register   Sign In
passing data from one controller function to another

I have an index function that loads a view with a form, this index function handles the validation of this form. If the validation succeeds I want the data to be passed along to complete a second form.

The way I currently have this setup is:

My index page is a form with form validation, if the validation succeeds the data of this form is sent to a different function within that same controller through url using redirect and passing data as arguments but my problem here is that i have to redo the validation all over again in the new function (and can't use form helper there).

How else could I send the data? The reason I want this other function to handle it is first its a whole different form of itself and second, I want the user to be able to quickly trigger my function from url.

Any help appreciated, please ask if you need clarification.

please post code.

I use varone and vartwo to call a function in func2() which retrieves data for options in the func2() form

One way to pass it is via session flashdata:

$this->session->set_flashdata('varone', 'value');

in the next controller:

function some_page()
  $varone = $this->session->flashdata('varone');

the data is available only (up to) the next server request.

Otherwise, you would code your controllers in the following manner:

function func2($varone = "", $vartwo = "")

where the statements inside func2:

$varone = "", $vartwo = ""

refers to the default value for these variables should func2 be
called when none are given.

Have I understood your problem correctly?

this is great, thanks!
do i need do add some validation to the data as it gets to the new function if it is stored in flash data? Is it easy for someone to tamper the data or is it stored on server.

My ideal situation is if the data is stored on server and never on client so they have no way to temper with it.

if you have already validated the data, there is little need for it on redirection to the next controller, perhaps just a check if it's empty or not. Of course, you are still encouraged to do some other checks, after validation (whether the value's relevant to the next controller, but you may do it before passing it).

Also, You can modify your

$submitone = $this->input->post('varone');
$submittwo = $this->input->post('vartwo');

codes to:

$submitone = $this->input->post('varone',TRUE);
$submittwo = $this->input->post('vartwo',TRUE);

to pass them through an XSS filter before setting them as flashdata.

Theme © iAndrew 2016 - Forum software by © MyBB