Welcome Guest, Not a member yet? Register   Sign In
Undefined variable error after form validated in Codeigniter
#1

[eluser]FlyingCat[/eluser]
I am trying to use form validation for my form after user submits it. My view page has a variable that was passed by my controller. The view page works fine until I submit the form to validate. It gave me errors saying the variables passed by controller were undefined. I am not sure how to solve this. Any thoughts? Thanks a lot!

Code:
Controller - status.php
    
    
        public function load_view ()
          {
           $this->load->model('project_query');
           $JNresults=$this->project_query->get_jobnumber();
          
           $data['JNresults']=$JNresults->result(); //pass to view
           $data['view']='form_view';
           $this->load->view('include/template',$data);
          }
    
        public function validate ()
                    {
                           //validate form
                            ...
                            ...
                            
                            $data['error']='There are errors in your form.';
                            $data['view']='form_view';
           $this->load->view('include/template',$data);
    
                    }

    View page - form_view.php
    
        foreach ($JNresults as $row):    //work fine when first loaded.
           echo $row->job_number;
         endforeach;
    
            echo form_open('status/validate');
      echo validation_errors();
      //echo input field
                ....
                ....    
      echo form_submit($submit);
      
      echo form_close();
I will get undefined variable $JNresults error after I submit form. I understand the view page don't recognize it because it was passed from the controller. I just want to know if anyone can help me to solve this. Thanks!

#2

[eluser]Unknown[/eluser]
Hey,

Your form is calling your validate() method from status.php, in the code you have supplied above you are not defining $data['JNresults'] in that function.

Are you expecting to have a JNresults value after form submission - if so you will need to define it again within the submission method.

If you are not exepecting to show JNresults after submission then edit your view to only run the foreach if the value is set.

Code:
if (isset($JNresults)
{
foreach ($JNresults as $row):    //work fine when first loaded.
           echo $row->job_number;
         endforeach;
}

HTH
Dave
#3

[eluser]FlyingCat[/eluser]
Got you. Thanks for the help.
#4

[eluser]InsiteFX[/eluser]
Code:
// makes the $data global to all views.
$this->load->vars($data);
$this->load->view('include/template');




Theme © iAndrew 2016 - Forum software by © MyBB