首页 > 解决方案 > 输入类型提交点击时不会触发

问题描述

我的代码有问题,我想创建一个上传按钮来上传我的 .xlsx 文件,但即使我尝试单击它也不会触发。

我的 view.php :

<form action="<?php echo base_url("index.php/fin/cost_control/workingcanvas/import"); ?>" method="post" id="import_form" enctype="multipart/form-data">
    <p><label>Pilih File Excel</label>
    <input type="file" name="file" id="file" required accept=".xls, .xlsx" /></p>
    <br />
    <input type="submit" id="import" name="import" value="Import" class="btn btn-info" />
</form>

我的控制器:

public function import(){
   include APPPATH.'third_party/PHPExcel/PHPExcel.php';
   $excelreader = new PHPExcel_Reader_Excel2007();
   $loadexcel = $excelreader->load('excel/'.$this->filename.'.xlsx');
   $sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);
   $numrow = 1;
   foreach($sheet as $row){
     if($numrow > 1){
       array_push($data, array(
         'nis'=>$row['A'],
         'nama'=>$row['B'],
         'jenis_kelamin'=>$row['C'],
         'alamat'=>$row['D'],
       ));
     }
     $numrow++;
    }
  $this->SiswaModel->insert_multiple($data);
  redirect("Siswa");
}

和我的模型:

public function insert_multiple($data){
   $this->db->insert_batch('siswa', $data);
}

我尝试在自动加载上添加一些部分,例如

$autoload['helper'] = array('url','form','file');

有什么建议或建议吗?谢谢。

标签: codeignitercodeigniter-3import-from-excel

解决方案


首先在codeigniter的自动加载文件中加载表单助手或手动加载

$autoload['helper'] = array('url'); // autoload
$this->load->helper->('form'); // manually loading form helper

你最好用这个

$attributes = array('enctype' => 'multipart/form-data');
echo form_open('index.php/fin/cost_control/workingcanvas/import', $attributes);

<your upload form code>

<?php echo form_close(); ?>

而不是标签

更多信息:https ://codeigniter.com/user_guide/helpers/form_helper.html


推荐阅读