You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter表单提交重定向报错:Undefined variable: laporan

Fixing "Undefined variable: laporan" Error in CodeIgniter Form Redirect

Let's break down what's causing this error and how to fix it quickly:

The Root Cause

Your detaillaporan.php view is trying to use the $laporan variable, but when you handle form submissions in the beriRespon() method (either validation fails or succeeds), you're not passing this variable to the view. Unlike your detailLaporan() method, which properly fetches the laporan data and adds it to the $data array, the beriRespon() method is missing this critical step.

Step-by-Step Fix

Update your beriRespon() method to include the missing data for the view:

  1. For the validation failure branch:
    Inside the if ($this->form_validation->run() == FALSE) block, add the code to fetch laporan details and related data:

    if ($this->form_validation->run() == FALSE) {
        // Add these lines to pass required data to the view
        $data['laporan'] = $this->Home_model->getLapDetail($kode_laporan);
        $data['respon'] = $this->Home_model->getResponInfo($kode_laporan)->result_array();
        $data['rsp'] = $this->Home_model->getResponInfo($kode_laporan)->num_rows();
        
        $data['page'] = 'detaillaporan';
        $data['kode_laporan'] = $kode_laporan;
        $this->load->view('home/master', $data);
    }
    
  2. For the validation success branch:
    Similarly, add the same data fetching code before loading the view in the else block:

    else {
        // ... existing upload and inputRespon code ...
        
        // Add these lines to pass required data to the view
        $data['laporan'] = $this->Home_model->getLapDetail($kode_laporan);
        $data['respon'] = $this->Home_model->getResponInfo($kode_laporan)->result_array();
        $data['rsp'] = $this->Home_model->getResponInfo($kode_laporan)->num_rows();
        
        $data['message'] = 'Laporan berhasil dikirim!';
        $data['redir'] = 'laporan/'.$kode_laporan;
        $data['page'] = 'detaillaporan';
        $this->load->view('home/master', $data);
    }
    

Bonus: Reduce Code Duplication

To avoid repeating the same data-fetching code in two methods, you can create a private helper method in your controller:

private function getLaporanContext($kode_laporan) {
    $context = [];
    $context['laporan'] = $this->Home_model->getLapDetail($kode_laporan);
    $context['respon'] = $this->Home_model->getResponInfo($kode_laporan)->result_array();
    $context['rsp'] = $this->Home_model->getResponInfo($kode_laporan)->num_rows();
    $context['id'] = $this->session->userdata('id');
    $context['kode_laporan'] = $kode_laporan;
    return $context;
}

Then use it in both detailLaporan() and beriRespon():

// In detailLaporan()
$data = $this->getLaporanContext($kode_laporan);
$data['page']='detaillaporan';
$this->session->set_userdata('kode_laporan', $kode_laporan); // Fixed: set_userdata takes key and value
$this->load->view('home/master', $data);

// In beriRespon() validation failure
$data = $this->getLaporanContext($kode_laporan);
$data['page'] = 'detaillaporan';
$this->load->view('home/master', $data);

Note: I also fixed a small issue in your original detailLaporan() method: $data['kode_laporan'] = $this->session->set_userdata('kode_laporan'); should be $this->session->set_userdata('kode_laporan', $kode_laporan); because set_userdata() returns a boolean, not the value you're trying to store.

内容的提问来源于stack exchange,提问作者R. Amalia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:05:06