CodeIgniter中同一视图上的两个表单

时间:2017-04-10 07:24:44

标签: php codeigniter

我在Codeigniter中遇到问题,因为我在同一个视图/网站上有两个上传表单。似乎Codeigniter只承认从一个表格提交。即使我在表单中更改输入名称,问题仍然存在。 以下是我的观点和控制器:

查看:

<?php echo form_open_multipart('admin/upload/do_upload_ac');?>
                    <input type="file" name="actives"  />
                    <br /><br />
                    <input class="btn btn-primary" type="submit" value="Add Active Emails" />
                    </form>

<?php echo form_open_multipart('admin/upload/do_upload_un');?>
                    <input type="file" name="unactives"  />
                    <br /><br />
                    <input class="btn btn-primary" type="submit" value="Add Active Emails" />
                    </form>   

和控制器:

public function do_upload_ac()
    {

            header('Cache-Control: must-revalidate');
            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = 'csv';
            $config['max_size']             = 0;
            $config['max_width']            = 1024;
            $config['max_height']           = 768;

            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload('actives'))
            {
                    $error = array('error' => $this->upload->display_errors());
                    $this->session->set_flashdata('error', 'Csv Data Import failed!! Please check the file size or file type!');
                    //$this->load->view('admin/muc', $error);
                    redirect(base_url() . 'index.php/admin/muc');
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());

                    $filename_ac = $this->upload->data('full_path');

                    $this->Upload_model->insert_csv_ac($filename_ac);
                    $this->Upload_model->clean_temp_ac();
                    $this->Upload_model->truncate_temp_ac();             

                    $this->session->set_flashdata('success', 'Csv Data Imported Succesfully!');
                    //$this->load->view('success', $data);
                    //unlink($filename_ac); 
                    redirect(base_url() . 'index.php/admin/muc');
            }


    }

    public function do_upload_un()
    {
            header('Cache-Control: must-revalidate');                
            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = 'csv';
            $config['max_size']             = 0;    



            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload('unactives'))
            {
                    $error = array('error' => $this->upload->display_errors());
                    $this->session->set_flashdata('error', 'Csv Data Import failed!! Please check the file size or file type!');
                    //$this->load->view('admin/muc', $error);
                    redirect(base_url() . 'index.php/admin/muc');
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());

                    $filename_ac = $this->upload->data('full_path');

                    $this->Upload_model->insert_csv_un($filename_un);
                    $this->Upload_model->clean_temp_un();
                    $this->Upload_model->truncate_temp_un();             

                    $this->session->set_flashdata('success', 'Csv Data Imported Succesfully!');
                    //$this->load->view('success', $data);
                    unlink($filename_un); 
                    redirect(base_url() . 'index.php/admin/muc');
            }


    }

我在这里搜索类似的问题,但我刚发现一次上传多个文件。有任何想法吗?谢谢大家。

2 个答案:

答案 0 :(得分:0)

好的,为了让您了解多次提交,我已经为您呈现了一个示例视图。我创建了以下视图

enter image description here

使用此代码

    <div class="container">
    <div class="col-md-3" style="box-shadow: 2px 2px 2px grey">
        <form action="<?php echo base_url().'admin/upload/do_upload_ac'?>" method="post" id="form1">
            <div class="form-group">
                <label > Upload 1</label>
                <input type="file" name="file1" >
            </div>
            <div class="form-group">
                <button type="button" class="btn btn-danger" onclick="submitOne()">Upload Image1</button>
            </div>
        </form>
    </div>
    <div class="col-md-3" style="box-shadow: 2px 2px 2px grey">
        <form action="<?php echo base_url().'admin/upload/do_upload_un'?>" method="post" id="form2">
            <div class="form-group">
                <label > Upload 2</label>
                <input type="file" name="file1" >
            </div>
            <div class="form-group">
                <button type="button" class="btn btn-danger" onclick="submitTwo()">Upload Image2</button>
            </div>
        </form>
    </div>
</div>

这个脚本

<script>
function submitOne()
{

    $('#form1').submit();
}
function submitTwo()
{

    $('#form2').submit();
}
</script>

我按第一个按钮

enter image description here

同样如果我按下第二个按钮,它会显示我将提交form2。提交表单后您执行的操作全部位于您提交表单的URL中。

答案 1 :(得分:0)

您的代码正在运行,无需更新。但是您应该使用codeigniter的form_close()或用户html表单标签更改html表单以开始表单。