CodeIgniter表单提交重定向报错:Undefined variable: laporan
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:
For the validation failure branch:
Inside theif ($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); }For the validation success branch:
Similarly, add the same data fetching code before loading the view in theelseblock: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

