Search code examples
phphtmlcodeignitercodeigniter-3codeigniter-2

How can I keep variable value if validation fails in codeigniter


I want to validate a form to make sure a user entered something in the description field in this situation form validation is correct.

But,

Here , I pass value to function 2 by fetching values from function 1

When function 2 loads first time it fetch data and display the values (OK)

But When function 2 resubmit for validation and submision data these valitable get empoty and it throw the error Message: Undefined variable: query (ERROR)

I don't know how to do this, hehe. I'm just a newbie PHP programmer.

Here's what I have so far:

function 1

public function ask()
{
$this->load->model('MainM');
$this->load->library('form_validation');
$this->form_validation->set_rules('index', 'AL Index', 'trim|required|min_length[7]|max_length[7]');
if($this->form_validation->run() == FALSE) {
        $this->load->view('enterindex');
    }else{
        $query = null; //emptying in case 
        $index = $this->input->post("index"); //getting from post value
        $query = $this->db->get_where('tbl_reg', array('reg_index' => $index));         
        $count = $query->num_rows(); //counting result from query           
        if ($count == 1) {
        $data['query'] = $this->MainM->getregdata($index);  
        $result = $this->load->view('submitques',$data);
    }else{
        $data = array();
        $data['error'] = 'error';
        $this->load->view('enterindex', $data);
    }   
}

function 2

public function submitq()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('yourq', 'Question', 'required|min_length[7]'); 
    if($this->form_validation->run() == FALSE) {            
        $this->load->view('submitques',$data);
    } else {        
     //insert the submitq form data into database
        $data = array(
            'reg_id' => $this->input->post('reg_id'),
            'ques' => $this->input->post('yourq'),
            'call' => "yes",
            'status' => "New",
            'date' => date('Y-m-d H:i:s')
        );
            if ($this->db->insert('tbl_ques', $data))                   
        {
            // success
           $this->session->set_flashdata('success_msg', 'Your Submission is success');
           redirect('main/submitq');
        }
        else
        {
            $this->load->view('submitques', $data);
        }     
    }   

}

Solution

  • Actully you have $data variable put in else part so thats why this error appeared you need to define before loop.

    public function submitq()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('yourq', 'Question', 'required|min_length[7]'); 
    // define here before check condtions
        $data = array(
            'reg_id' => $this->input->post('reg_id'),
            'ques' => $this->input->post('yourq'),
            'call' => "yes",
            'status' => "New",
            'date' => date('Y-m-d H:i:s')
        );
        if($this->form_validation->run() == FALSE) {            
            $this->load->view('submitques',$data);
        } else {        
            //insert the submitq form data into database
                if ($this->db->insert('tbl_ques', $data))                   
            {
                // success
                $this->session->set_flashdata('success_msg', 'Your Submission is success');
                redirect('main/submitq');
            }
            else
            {
                $this->load->view('submitques', $data);
            }     
        }   
    
    }