Welcome Guest, Not a member yet? Register   Sign In
Multipage Form using Sessions
#1

[eluser]bubaphex[/eluser]
Hi guys,

Farly new to CI, Trying to a multipage form using sessions. Having trouble bring over the data from page1 to page2 and submitting it into the db

Here what i looks like, any help is much appreciate

Form Controller
Code:
<?php

class Form extends controller {
    
    function Form() {
        parent::Controller();
    }
    
    function index()
    {
        $data = array('formdata' => $this->session->userdata('formdata'));
        $this->load->view('form1', $data);
    }
    
    function page1()
    {

        // Load Validation Lib
        $this->load->library('form_validation');
        
        // Page 1 fields to be validated
        $this->form_validation->set_rules('make', 'Make', 'trim|required');
        $this->form_validation->set_rules('model', 'Model', 'trim|required');
        $this->form_validation->set_rules('year', 'Year', 'trim|required');
        
        if($this->form_validation->run() == FALSE)
                {
                    $this->load->view('form1');
                }

                else
                {            
                    $data = array('formdata' => $this->session->userdata('formdata'));
                    $this->load->view('form2', $data);
                }
        
    }
    
    function page2()
    {
        
        // Load Validation Lib
        $this->load->library('form_validation');
        
        // Page 2 fields to be validated
        $this->form_validation->set_rules('name', 'Name', 'trim|required');
        $this->form_validation->set_rules('address', 'Address', 'trim|required');
        $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');


            if($this->form_validation->run() == FALSE)
            {
                $this->load->view('form2');
            }

            else
            {            
                $this->load->model('import_model');

                    if($query = $this->import_model->add())
                            {
                                echo 'imported fine'; //echo for testing purposes
                            }
                            else
                            {
                                echo 'import went wrong'; //echo for testing purposes            
                            }
                
            }
    
        
        
    }
}

View Page 1
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;style type="text/css"&gt;

        input {
            display:block;
            margin:5px;
        }

&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
    
    <h1>Page 1</h1>

    <fieldset>
    <legend>Page 1 Info</legend>

    &lt;?php
    
    echo form_open('form/page1');
    
    echo form_input('make', set_value('make', $formdata['make']));
    echo form_input('model', set_value('model', 'Model'));
    echo form_input('year', set_value('year', 'Year'));

    echo form_submit('submit', 'Next Page');
    
    echo form_close();
    
    ?&gt;

    &lt;?php echo validation_errors('<p class="error">'); ?&gt;
    </fieldset>
    
&lt;/body&gt;
&lt;/html&gt;

View Page 2

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;style type="text/css"&gt;

        input {
            display:block;
            margin:5px;
        }

&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
    
    <h1>Page 2</h1>

    <fieldset>
    <legend>Page 2 Info</legend>

    &lt;?php
    echo form_open('form/page2');
    
    echo form_input('name', set_value('name', 'Full Name'));
    echo form_input('address', set_value('address', 'Address'));
    echo form_input('email', set_value('email', 'Email Address'));

    echo form_submit('submit', 'Send');
    
    echo form_close();
    
    ?&gt;

    &lt;?php echo validation_errors('<p class="error">'); ?&gt;
    </fieldset>
    
&lt;/body&gt;
&lt;/html&gt;

Model

Code:
&lt;?php

class Import_model extends Model {

    
    function add()
    {
        
        $new_member_insert_data = array(
            //Page 1
            'make' => $this->input->post('make'),
            'model' => $this->input->post('model'),
            'year' => $this->input->post('year'),
            
            //Page 2
            'name' => $this->input->post('name'),
            'address' => $this->input->post('address'),
            'email' => $this->input->post('email'),                                
        );
        
        $insert = $this->db->insert('data', $new_member_insert_data);
        return $insert;
    }
}
#2

[eluser]markup2go[/eluser]
Hi and welcome to CI! After your validation has passed you will want to insert the posted data into the session. I didn't see that going on in your example code. Have a read here: http://ellislab.com/codeigniter/user-gui...sions.html because in order to grab session data you have to put it there first!
#3

[eluser]bubaphex[/eluser]
Hey man,

thanks for the reply.

Should i be just addeding in

$this->session->userdata('formdata'); to my page2 method ?
#4

[eluser]markup2go[/eluser]
[quote author="bubaphex" date="1256615978"]Hey man,

thanks for the reply.

Should i be just addeding in

$this->session->userdata('formdata'); to my page2 method ?[/quote]

If you plan on doing something with the data from page 1 then yes. If not, just leave it in the session until you are ready to do something with it.
#5

[eluser]bubaphex[/eluser]
ahh so i should put it in the model then?

Quote:&lt;?php

class Import_model extends Model {


function add()
{

$new_member_insert_data = array(
//Page 1
'make' => $this->session->userdata('formdata'),
'model' => $this->input->post('model'),
'year' => $this->input->post('year'),

//Page 2
'name' => $this->input->post('name'),
'address' => $this->input->post('address'),
'email' => $this->input->post('email'),
);

$insert = $this->db->insert('data', $new_member_insert_data);
return $insert;
}
}

I've tried it this way doesnt seam to work for me
#6

[eluser]bigtony[/eluser]
[quote author="bubaphex" date="1256615978"]
Should i be just addeding in

$this->session->userdata('formdata'); to my page2 method ?
[/quote]
Note that $this->session->userdata just READS the value in the session variable. To store it, you need to use $this->session->set_userdata('formdata', $yourformdata);.
#7

[eluser]mjsilva[/eluser]
You might want to check this Jquery plugin: http://www.jankoatwarpspeed.com/post/200...query.aspx




Theme © iAndrew 2016 - Forum software by © MyBB