CodeIgniter Forums
Logic for multi-step form.. not working. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Logic for multi-step form.. not working. (/thread-33761.html)

Pages: 1 2


Logic for multi-step form.. not working. - El Forum - 09-07-2010

[eluser]tomclowes[/eluser]
Ok my logic for a multi step form is as follow.

A session value which contains the stage.

- if no stage
- if form 1 not validated
- display stage 1
- if form 1 validated
- set session to step2

- if step 2
- if step 2 not validated
- show step 2
- if form 2 validated
- set session to complete

- if complete
- show thanks message

What is happening is as follows. I submit form 1 correctly, it loads the complete view.
So basically it is setting session to step 2, saying step 2 is validated, setting session to complete, and loading thankyou message. I.E The step 2 validation is not working.

This is the frame of the code:

Code:
if(!$this->session->userdata('form_stage'))
        {

            if ($this->form_validation->run('suggest_1') == FALSE)
            {


            }
            else
            {
                
                $this->session->set_userdata('form_stage', 'step_2');
                
            }
        }
        
        if($this->session->userdata('form_stage')=='step_2')
        {
            if ($this->form_validation->run('suggest_2') == FALSE)
            {

            }
            else
            {
                
                $this->session->set_userdata('form_stage', 'complete');
                
            }
        }
        
        if($this->session->userdata('form_stage')=='complete')
        {

        }


Any Ideas?

I've spent hours on this without success Sad

Thanks


Logic for multi-step form.. not working. - El Forum - 09-08-2010

[eluser]smilie[/eluser]
Well, you have a logic error in your code.
Your code:

Code:
if(!$this->session->userdata('form_stage'))
        {

            if ($this->form_validation->run('suggest_1') == FALSE)
            {


            }
            else
            {
                # Here you set form_stage to 'step_2'
                $this->session->set_userdata('form_stage', 'step_2');
                
            }
        }
        
        # And here, you are checking if form_stage is 'step_2' - Which it IS! So code moves from here further on, setting bellow session to complete :-)
        if($this->session->userdata('form_stage')=='step_2')
        {
            if ($this->form_validation->run('suggest_2') == FALSE)
            {
                
            }
            else
            {
                
                $this->session->set_userdata('form_stage', 'complete');
                
            }
        }
        
        if($this->session->userdata('form_stage')=='complete')
        {

        }

See my comments above.

What I have done in one of order pages I created is:

Code:
function linux($step='1',$pck_id='0')
    {
        if($step == '' || empty($step))
        {
           # No step defined show error
        }

        /*
         * Step 1
         */
        if($step == 1)
        {
            # Show step 1 page
        }
        if($step == 2)
        {
            # Show step 2 page
        }
        if($step == 3)
        {
            # Last step, finish page
        }
     }

$step is simply param in the url, i.e.
domain.com/linux/1
Link to next page is: domain.com/linux/2 etc.
Of course, I place also some session data in it to double check data...

Hope this helps.

Regards,
Smilie


Logic for multi-step form.. not working. - El Forum - 09-08-2010

[eluser]tomclowes[/eluser]
Thanks for the response, and suggestion.

If possible could you explain my logic error in detail?

I am saying if form 1 is done, set form_stage, which in turn allows the loading of step 2.
IE if step 1 done, load step 2...

As far as I can see my technique should work?

Thanks


Logic for multi-step form.. not working. - El Forum - 09-08-2010

[eluser]smilie[/eluser]
Hi,

As far I can see in your code, it is all one function. Where do you call view?

If view is called at the end of that if loop - then your mistake that while within the loop step1 becomes step2 -> which then triggers next if() in a code.

Could you re-post your code, but this time include views please.

Regards,
Smilie


Logic for multi-step form.. not working. - El Forum - 09-08-2010

[eluser]tomclowes[/eluser]
The idea is to have everything in one page http://domain.com/controller_name

The view that is loaded is simply dependent on the session keys/form validation.

This is the extent of my code at this point as it has not been working as expected.

Thanks



Code:
function index()
{


if(!$this->session->userdata('form_stage'))
        {

            if ($this->form_validation->run('suggest_1') == FALSE)
            {

//LOAD FORM 1 VIEW

            }
            else
            {
                
                $this->session->set_userdata('form_stage', 'step_2');
                
            }
        }
        
        if($this->session->userdata('form_stage')=='step_2')
        {
            if ($this->form_validation->run('suggest_2') == FALSE)
            {
//LOAD FORM 2 VIEW
            }
            else
            {
                
                $this->session->set_userdata('form_stage', 'complete');
                
            }
        }
        
        if($this->session->userdata('form_stage')=='complete')
        {
//LOAD COMPLETE VIEW
        }  
}



Logic for multi-step form.. not working. - El Forum - 09-08-2010

[eluser]smilie[/eluser]
Hi,

I am not sure if the script ends when it comes to $this->load->view('');

Could you try following:
Add
echo "test"; exit;

just after loading your view and call the controller. In case you see "test" in the screen, then it means script continues after view has been loaded. As next line in your code again sets your session variable, it is a good explanation why you see "complete" page. This also means that you will not be able to work with sessions like that. Maybe that you can exit script once view is loaded?

Do a test and let me know.

Regards,
Smilie


Logic for multi-step form.. not working. - El Forum - 09-08-2010

[eluser]tomclowes[/eluser]
That does not make sense. The age does NOT exit after a view is called.

So.. I complete step 1 properly. The session var is set to step 2. Because no step 2 form has been submitted if ($this->form_validation->run('suggest_2') WILL EQUAL FALSE), and form2 will be loaded. Nothing else should be loaded.

What is actually happening is you fill in form one, and the COMPLETE VIEW is being loaded, which means the session is set to complete, which means that the form validation for suggest 2 is returning TRUE. This is odd considering that at this point the user hasnt submitted form 2 Smile

THANKS


Logic for multi-step form.. not working. - El Forum - 09-08-2010

[eluser]smilie[/eluser]
Tom,

Yes I see now what you mean (I am a bit ill and tired so didn't caught it first time).
Is your form config OK (suggest_2)?

Could you place:

Code:
if ($this->form_validation->run('suggest_2') == FALSE)
            {
                 echo "run suggest_2 false"; exit;
            }
            else
            {
                 echo "run suggest_2 true"; exit;
            }

just to make sure you are getting in TRUE statement. Then you can debug the suggest_2 form validation rules / config.

Regards,
Smilie


Logic for multi-step form.. not working. - El Forum - 09-08-2010

[eluser]tomclowes[/eluser]
Not to worry.

To test, I put the code you provided in a controller of its own. It returned 'FALSE' - The config file is working. No form was submitted, therefore of course it would return false.

From my testing i have narrowed it down some more.

The problem seems to be as follows. When I validate a form, all other form validations in the file return true, even if they are not true. Is it not possible to have multiple validation calls in one file?

Thx


Logic for multi-step form.. not working. - El Forum - 09-08-2010

[eluser]smilie[/eluser]
I have also controllers with more then 1 validation call in it (within same function). Have no problems with it. However I do not work with form config, I just fire up $this->form_validation->run() (thus without form name in it).

As your form validations are also split within different if statements, could you try to remove that parameter and just fire run()?

Regards,
Smilie