Welcome Guest, Not a member yet? Register   Sign In
Help with Multi-Step Form using Sessions
#1

[eluser]byrnef87[/eluser]
Hi,

I've been using Codeigniter for a recent tuition centre management system and I'm having some trouble with a multi-step form.

The form is to enrol a family of students in various classes, so I iterate over the same student enrolment details form an arbitrary number of times, storing the previous enrolment details as a session variable. This all seems to work fine (the confirmation page shows all personal and class details for each student). However when I call a new method in the controller to load a page that displays receipts of the enrolments (the method is "finalizeEnrolment"), the session only seems to have stored the first student's details.

I have scoured my code and many forums for an answer to this, but I'm stumped. I hope you can help.

Here are the relevant controller methods:

Code:
/**
         *  Display a form for the user to fill in a student's personal and
         *  class details and return these enrolment details.
         *
         * @return <type> The student's enrolment details
         */
        function enrol()
        {

            // Check if user is logged in, if not redirect to login page
            if(!$this->session->userdata('logged_in'))
            {
                redirect( 'login');
            }

            if ( isset( $_POST['firstname'] ) )
                $family_enrolment = $this->session->userdata('enrolment');
            else
                $family_enrolment = array();

            if ( isset($_POST['finishEnrolButton']) )
            {
                $family_enrolment[] = $this->getStudentEnrolment();
                $this->session->set_userdata('enrolment', $family_enrolment);

                $data['title'] = 'Enrol';
                $data['head'] = link_tag('system/application/views/css/reg_form.css');
                $data['nav_list'] = array(); //empty nav as it is a register page
                $data['content'] = $this->load->view('family/enrol_confirm_view', array(), true);
                $this->load->view('template_view', $data);
            }
            else
            {
                if ( isset( $_POST['addStudentButton'] ) )
                {
                    $family_enrolment[] = $this->getStudentEnrolment();
                    $this->session->set_userdata('enrolment', $family_enrolment);
                }

                $data['title'] = 'Enrol';
                $data['head'] = link_tag('system/application/views/css/reg_form.css')
                   . trim($this->load->view( 'js/js_enrolform', array(), true ))
                   . trim($this->load->view( 'js/js_datepicker', array('id' => 'dateofbirth'), true ));
                $data['nav_list'] = array(); //empty nav as it is a register page
                $data['content'] = $this->load->view('family/enrol_view', array(), true);
                $this->load->view('template_view', $data);
                
            }
        }

        function finalizeEnrolment()
        {

            // Check if user is logged in, if not redirect to login page
            if(!$this->session->userdata('logged_in'))
            {
                redirect( 'login');
            }

            if ( isset( $_POST['cancelButton'] ) )
            {
                $this->session->unset_userdata('enrolment');
                redirect('family/enrol');
               //print_r($this->session->userdata('enrolment'));
            }
            else if ( isset( $_POST['confirmButton'] ) )
            {
                $this->load->model('users_model');
                
                $finalize_data['FamilyId'] = $this->users_model->getFamilyId(
                    $this->session->userdata('UserId') );

                $finalize_data['enrolment'] = $this->session->userdata('enrolment');

                $data['title'] = 'Enrol';
                $data['head'] = link_tag('system/application/views/css/reg_form.css');
                $data['nav_list'] = array(); //empty nav as it is a register page
                $data['content'] = $this->load->view('family/enrol_finalize_view', $finalize_data, true);
                $this->load->view('template_view', $data);
            }
            else
                redirect('family/enrol');

        }

Here are the relevant views:

enrol_confirm_view (working):

Code:
<div id='div-regForm'>
    <div class="form-title">Confirm Enrolment</div>

    &lt;?php echo form_open("family/finalizeEnrolment"); ?&gt;

    &lt;?php $enrolment = $this->session->userdata('enrolment'); ?&gt;

    &lt;?php print_r($enrolment); ?&gt;

...

    &lt;input id="cancelButton" name="cancelButton" type="submit" value="Cancel Enrolment" style="width: 140px" tabindex="14" /&gt;
    &lt;input id="confirmButton" name="confirmButton" type="submit" value="Submit & Pay at Counter" style="width: 180px" tabindex="14" /&gt;

    &lt;?= form_close(); ?&gt;

The line "print_r($enrolment);" here displays every student's details.


enrol_finalize_view (not working):

Code:
<div id='div-regForm'>
    <div class="form-title">Thank You</div>

    <p>Thank you for your enrolment. Please bring the print-out to the pay
        station to finalise your payment and to pick up your books.</p>
    
    &lt;?php echo form_open("family/finalizeEnrolment"); ?&gt;

    &lt;?php $enrolment = $this->session->userdata('enrolment'); ?&gt;

    &lt;?php print_r($enrolment); ?&gt;
    ....

The line "print_r($enrolment);" here only displays details for the first student.

I've also tried moving the code for "finalizeEnrolment" into the "enrol" method, however it gave the same problem.

It's probably something trivial, but I can't work it out for the life of me!

Thanks a lot for reading all this!
#2

[eluser]InsiteFX[/eluser]
the user guide

Session Flashdata

Enjoy
InsiteFX
#3

[eluser]byrnef87[/eluser]
Hi InsiteFX,

I've read the guide, and as you can see from my code, I'm not loading the data into Flashdata, so there's no reason why it would be automatically cleared, is there? Unless, does setting $this->session->userdata count as Flashdata?

Thanks
#4

[eluser]InsiteFX[/eluser]
Flashdata wil store the data to the next page when you leave that page it is wipped out you can then in that page tell it to the data one more time. Also did you try to do a new session_start on the other page?

try it.

Enjoy
InsiteFX

P.S. Sorry for getting back to so late I am back to work now and I do not get home until after 1:00 am in the morning.




Theme © iAndrew 2016 - Forum software by © MyBB