如何存储多个表单数据并将其发送到下一个视图?

时间:2015-08-06 09:46:53

标签: php forms codeigniter

我正在使用codeigniter。我有三个不同的标签窗口,我在每个页面中都有单独的表单。我需要存储我的第一个表单内容并将其从第二个内容发布到控制器。类似地,在填充这些内容后的第三页上,我需要将它们插入到我的数据库中的表上。 这是我的控制器页面。

<?php
class Admin_employee extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('employee_model');
    $this->load->model('add_service_model');
        $this->load->model('designation_model');

        if(!$this->session->userdata('is_logged_in')){
            redirect('admin/login');
        }
    }
     public function index()
    {
        $this->load->library('session');
        $this->session->unset_userdata('tax_details');
        //all the posts sent by the view
        $manufacture_id = $this->input->post('category');        
        $search_string = $this->input->post('search_string');        
        $order = $this->input->post('order'); 
        $order_type = $this->input->post('order_type'); 

        //pagination settings
        $config['per_page'] = 999999;
        $config['base_url'] = base_url().'admin/employee';
        $config['use_page_numbers'] = TRUE;
        $config['num_links'] = 20;
        $config['full_tag_open'] = '<ul>';
        $config['full_tag_close'] = '</ul>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a>';
        $config['cur_tag_close'] = '</a></li>';


        //math to get the initial record to be select in the database
        $limit_end = ($page * $config['per_page']) - $config['per_page'];
        if ($limit_end < 0){
            $limit_end = 0;
        } 



    }//index


    public function form1()
    {

        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
             $this->form_validation->set_rules('id', 'id');

            $this->form_validation->set_rules('emp_first_name', 'emp_first_name','required');
            $this->form_validation->set_rules('emp_last_name', 'emp_last_name','required');
            $this->form_validation->set_rules('emp_email_id', 'emp_email_id');
            $this->form_validation->set_rules('emp_emergency_contact', 'emp_emergency_contact');
            $this->form_validation->set_rules('category', 'category');
            $this->form_validation->set_rules('emp_id_card', 'emp_id_card');

            $this->form_validation->set_rules('emp_date_of_hire', 'emp_date_of_hire');
            $this->form_validation->set_rules('emp_date_of_termination', 'emp_date_of_termination');
            $this->form_validation->set_rules('emp_date_of_rehire', 'emp_date_of_rehire');
            $this->form_validation->set_rules('emp_reference_num', 'emp_reference_num');
            $this->form_validation->set_rules('emp_service_limitation', 'emp_service_limitation');
            $this->form_validation->set_rules('emp_active', 'emp_active');
            $this->form_validation->set_rules('chair_renter', 'chair_renter');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

            //if the form has passed through the validation
            if ($this->form_validation->run())
            {
                $data_to_store = array(
                     'id' => $this->input->post('id'),
                    'emp_first_name' => $this->input->post('emp_first_name'),
                    'emp_last_name' => $this->input->post('emp_last_name'),
                    'emp_email_id' => $this->input->post('emp_email_id'),
                    'emp_emergency_contact' => $this->input->post('emp_emergency_contact'),          
                    'category' => $this->input->post('category'),
                    'emp_id_card' => $this->input->post('emp_id_card'),
                    'emp_time_in' => $this->input->post('emp_time_in'),
                    'emp_time_out' => $this->input->post('emp_time_out'),
                    'emp_date_of_hire' => $this->input->post('emp_date_of_hire'),
                    'emp_date_of_termination' => $this->input->post('emp_date_of_termination'),
                    'emp_date_of_rehire' => $this->input->post('emp_date_of_rehire'),
                    'emp_reference_num' => $this->input->post('emp_reference_num'),
                    'emp_service_limitation' => $this->input->post('emp_service_limitation'),
                    'chair_renter' => $this->input->post('chair_renter'),

                );



        }


         if($this->employee_model->store_employee($data_to_store)){
                    $data['flash_message'] = TRUE; 
                    $this->session->set_flashdata('flash_message', 'updated');
                }else{
                    $data['flash_message'] = FALSE; 



            }

        }
        $data['designation'] = $this->designation_model->get_designation();
        $data['main_content'] = 'admin/employee/add';
        $this->load->view('includes/template', $data);  
    } 
    public function form2()
    {
if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
             $this->form_validation->set_rules('id', 'id');

            $this->form_validation->set_rules('emp_active', 'emp_active');
            $this->form_validation->set_rules('chair_renter', 'chair_renter');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

            //if the form has passed through the validation
            if ($this->form_validation->run())
            {
                $data_to_store = array(
                     'id' => $this->input->post('id'),
                         'emp_active' => $this->input->post('emp_active'),
                'chair_renter' => $this->input->post('chair_renter'),

                );



        }


         if($this->employee_model->store_employee($data_to_store)){
                    $data['flash_message'] = TRUE; 
                    $this->session->set_flashdata('flash_message', 'updated');
                }else{
                    $data['flash_message'] = FALSE; 



            }

        }

    $data['main_content'] = 'admin/employee/service_limitations';
        $this->load->view('includes/template', $data);
    } 

} 

这里我有2个功能。 form1form2。当我以第二种形式保存数据时,我需要插入第一种形式和第二种形式的值。怎么做?有人可以帮我编码吗?

1 个答案:

答案 0 :(得分:0)

检查此工作流程

PHP控制器部分:

    public function __construct(){......  }

    public function index()
    {... }//index


    public function form1()
    {

        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
           $this->form_validation->set_rules('id', 'id');

           $this->form_validation->set_rules('emp_first_name', 'emp_first_name','required');
           $this->form_validation->set_rules('emp_last_name', 'emp_last_name','required');
           $this->form_validation->set_rules('emp_email_id', 'emp_email_id');
           $this->form_validation->set_rules('emp_emergency_contact', 'emp_emergency_contact');
           $this->form_validation->set_rules('category', 'category');
           $this->form_validation->set_rules('emp_id_card', 'emp_id_card');

           $this->form_validation->set_rules('emp_date_of_hire', 'emp_date_of_hire');
           $this->form_validation->set_rules('emp_date_of_termination', 'emp_date_of_termination');
           $this->form_validation->set_rules('emp_date_of_rehire', 'emp_date_of_rehire');
           $this->form_validation->set_rules('emp_reference_num', 'emp_reference_num');
           $this->form_validation->set_rules('emp_service_limitation', 'emp_service_limitation');
           $this->form_validation->set_rules('emp_active', 'emp_active');
           $this->form_validation->set_rules('chair_renter', 'chair_renter');
           $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

            //if the form has passed through the validation
           if ($this->form_validation->run())
           {
            $data_to_store = array(
               'id' => $this->input->post('id'),
               'emp_first_name' => $this->input->post('emp_first_name'),
               'emp_last_name' => $this->input->post('emp_last_name'),
               'emp_email_id' => $this->input->post('emp_email_id'),
               'emp_emergency_contact' => $this->input->post('emp_emergency_contact'),          
               'category' => $this->input->post('category'),
               'emp_id_card' => $this->input->post('emp_id_card'),
               'emp_time_in' => $this->input->post('emp_time_in'),
               'emp_time_out' => $this->input->post('emp_time_out'),
               'emp_date_of_hire' => $this->input->post('emp_date_of_hire'),
               'emp_date_of_termination' => $this->input->post('emp_date_of_termination'),
               'emp_date_of_rehire' => $this->input->post('emp_date_of_rehire'),
               'emp_reference_num' => $this->input->post('emp_reference_num'),
               'emp_service_limitation' => $this->input->post('emp_service_limitation'),
               'chair_renter' => $this->input->post('chair_renter'),

               );

}


// if($this->employee_model->store_employee($data_to_store)){
//     $data['flash_message'] = TRUE; 
//     $this->session->set_flashdata('flash_message', 'updated');
// }else{
//     $data['flash_message'] = FALSE; 
// }

}

//save this data to session 
$this->session->set_userdata('form1data', $data_to_store);
//redirect to next form

$data['designation'] = $this->designation_model->get_designation();
$data['main_content'] = 'admin/employee/add';
$this->load->view('includes/template', $data);  
} 

public function form2()
{
    if ($this->input->server('REQUEST_METHOD') === 'POST')
    {
       $this->form_validation->set_rules('id', 'id');

       $this->form_validation->set_rules('emp_active', 'emp_active');
       $this->form_validation->set_rules('chair_renter', 'chair_renter');
       $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

            //if the form has passed through the validation
       if ($this->form_validation->run())
       {
        $data_to_store = array(
           'id' => $this->input->post('id'),
           'emp_active' => $this->input->post('emp_active'),
           'chair_renter' => $this->input->post('chair_renter'),

           );

    }
}

    //save this data to session 
$this->session->set_userdata('form2data', $data_to_store);

//redirect to next form

}

//after all forms call this function
public function save_all_data_to_db() {
    $form1 = $this->session->userdata('form1data');
    $form2 = $this->session->userdata('form2data');

    //process and save everything to db
}

前端: 当用户完成第一个表单并单击下一个表单时,将所有表单数据发布到控制器。

通过javascript发布到控制器:

$(document).ready( function() {
    $('#somebutton').on('click', function() {
        $.post( "/form1", {
            "emp_first_name" : val,
            .
            .
            .
            .
        },function( data ) {
            //load next tab
        });
    });
});