可以从多个表单的单个表中插入多个数据吗?

时间:2015-07-30 07:05:09

标签: php mysql codeigniter

我正在使用codeigniter。我有多个表单但只有在我到达最后一个表单后才需要提交操作,该操作应插入所有表单中的所有数据。这该怎么做? 我有一个控制器文件。

<?php
class Admin_service extends CI_Controller {

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

        if(!$this->session->userdata('is_logged_in')){
            redirect('admin/login');
        }
    }


    public function add1()
    {

        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
$this->load->library('session');
            $this->session->unset_userdata('service_detail');

            $this->form_validation->set_rules('backbar', 'backbar');


            $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('service_detail'),
                    'service_name' => $this->input->post('backbar')

                );
                if($this->service_model->store_service($data_to_store)){
                    $data['flash_message'] = TRUE; 
                    $this->session->set_flashdata('flash_message', 'updated');

                }else{
                    $data['flash_message'] = FALSE; 
                }

            }

        }
        $data['category'] = $this->category_model->get_category();
        $data['main_content'] = 'admin/service/add1';
        $this->load->view('includes/template', $data);  

    }

    public function add()
    {
        if ($this->input->server('REQUEST_METHOD') === 'POST')
        {
    $this->load->library('session');
            $this->session->unset_userdata('service_detail');
            $this->form_validation->set_rules('id', 'id');
            $this->form_validation->set_rules('service_name', 'service_name','alpha');
            $this->form_validation->set_rules('category', 'category');
            $this->form_validation->set_rules('service_tax', 'service_tax');
            $this->form_validation->set_rules('service_length', 'service_length','numeric');
            $this->form_validation->set_rules('service_price', 'service_price','numeric');
            $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

            if ($this->form_validation->run())
            {
                $data_to_store = array(
                'id' => $this->input->post('id'),
                    'service_name' => $this->input->post('service_name'),
                    'category' => $this->input->post('category'),
                    'service_tax' => $this->input->post('service_tax'),
                    'service_length' => $this->input->post('service_length'),          
                    'service_price' => $this->input->post('service_price')
                );

                if($this->service_model->store_service($data_to_store)){
                    $data['flash_message'] = TRUE; 
                    $this->session->set_flashdata('flash_message', 'updated');
                    $this->session->set_userdata('service_detail', ['service_price'=>$this->input->post('service_price'),"service_tax"=>$this->input->post('service_tax')]);
                    redirect(base_url().'admin/service/view');
                }else{
                    $data['flash_message'] = FALSE; 
                }

            }

        }

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



}

在这里,我有两个函数addadd1,每个函数都加载一个单独的视图,我的表单。在add表单后,我需要从add1表单获取数据,并在我单击第二个表单中的提交按钮时插入单个表中。怎么做?有人可以帮我编码吗?

1 个答案:

答案 0 :(得分:0)

在用户提交表单的html page上添加一个复选框:

<input type="checkbox" name="is_finish" value="1" />
控制器中的

修改if ($this->form_validation->run())条件中的代码,如下所示:

if ($this->form_validation->run())
{

    $this->load->library('session');
    $service_details[] = array(
        'id' => $this->input->post('id'),
        'service_name' => $this->input->post('service_name'),
        'category' => $this->input->post('category'),
        'service_tax' => $this->input->post('service_tax'),
        'service_length' => $this->input->post('service_length'),          
        'service_price' => $this->input->post('service_price')
        );

    $service_details_sess = $this->session->userdata('service_details');

    if($service_details_sess != "") {
        $service_details[] = $service_details_sess;
    }

    $this->session->set_userdata('service_details',$service_details);

    $finish = $this->input->post('is_finish');

    if($finish == "1")
    {
        $this->service_model->store_service($service_details);
    }
}

修改store_service模型上的service_model功能,如下所示:

function store_service($service_details) {
    $this->load->library('session');
    $this->session->unset_userdata('service_details');
    $this->db->insert_batch($service_details);
    if ($this->db->affected_rows() > 0)
    {
        return TRUE;
    }
        return FALSE;
}

HTML部分:

<button id="store_data" type="button">Store</button>
<script type="text/javascript">
$("#store_data").click(function(event) {
    $.ajax({
        url: 'path to your controller that store data',
        type: 'POST',
        data: $("#your_form_id").serialize(),
    })
    .done(function() {
        console.log("success");
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });

});
</script>