0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks,you were right,i have to initalize $data['msg'] to null at the beginning :)
0
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.