Shahril Abu Bakar
Shahril Abu Bakar

Reputation: 35

Codeigniter passing variable error

I'm using CodeIgniter and having problem to send variable using the $this->load->view('signup',$data).

This is some code in my controller.

function create_member()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
    $this->form_validation->set_rules('password1', 'Confirm Password', 'trim|required|matches[password]');

    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('signup');
    }else
    {
        $this->load->model('membership');

        if($query=$this->membership->check_username() == TRUE)
        {
            $data['msg']= "Username exist";
            $this->load->view('signup',$data); //problem encounter here.

        }else
        {
            if($query = $this->membership->create_member())
            {
                $data['main_content'] = 'signup_successful';
                $this->load->view('include/template', $data);
            }else
            {
                $this->load->view('signup_form');
            }
        }
    }
}

And here the error message : A PHP Error was encountered
Severity: Notice
Message: Undefined variable: msg
Filename: views/signup.php
Line Number: 99

<?php echo $msg; ?> // code inside view/signup.php

Upvotes: 0

Views: 761

Answers (2)

gen_Eric
gen_Eric

Reputation: 227220

if($this->form_validation->run() == FALSE)
{
    $this->load->view('signup');
}

You're loading the view without passing any variables, so $msg won't be set.

You need to add isset($msg) to the view (or $data['msg'] = '' to the controller).

Upvotes: 0

Catfish
Catfish

Reputation: 19284

It's probably because you're code is getting into this block and you're not setting $data['msg'] in here so in the view it doesn't exist.:

else
        {
            if($query = $this->membership->create_member())
            {
                $data['main_content'] = 'signup_successful';
                $this->load->view('include/template', $data);
            }else
            {
                $this->load->view('signup_form');
            }
        }

Either initalize $data['msg'] to null at the beginning of your method, or do a check in the view to see if it exists.

Upvotes: 1

Related Questions