Welcome Guest, Not a member yet? Register   Sign In
Keep getting undefined variable error trying to pass variable into view
#1

Here is my controller

Code:
$this->form_validation->set_rules('chipNumber', 'Chip Number', 'required');
               $this->form_validation->set_rules('subNumber', 'Sub Number', 'required');
               $this->form_validation->set_rules('xLocation', 'X Location', 'required');
               $this->form_validation->set_rules('yLocation', 'Y Location', 'required');
              
               if($this->form_validation->run() == false) {
                    
                    $data['message'] = 'My Message';
                    
                    //$errorMessage = "Chip Number, Sub Number, X Location, Y Location combination (Part UI) already exists in the database for this Lot. Please try again with correct data.";
                    redirect(''.base_url().'parts_database/add_part/'.$this->input->post('lotID_FK').'/'.$this->input->post('runID_FK'), $data);


and here is my view

Code:
<?php
                                   if($data){
                                       foreach($data as $m){
                                           if($m->message){
                                               echo 'error message will go here';
                                           }
                                       }
                                   }
                               ?>


Essentially I'm trying to reload the view if form_validation fails, with the $data array set in the controller. It works ok except everytime I load the view, whether form_validation has had the chance to run or not I get an error that says Undefined Variable $data. Not sure what the right way is to go about this
Reply
#2

(This post was last modified: 08-09-2015, 09:52 AM by CroNiX.)

$data is only getting defined if the validation fails. Try just putting a
$data = array();

before the form validation so it will always exist.
Reply
#3

The array that you pass to a view (from within your controller), is automatically extracted.
If you assigned $data['message'] = ...
then your view will recognize $message, not $data.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB