我遇到了超过2小时的有线问题。我无法弄清楚。我试图从模型传递变量"errors"
来查看,但是当我尝试加载页面时,它显示错误,说"undefined variable: errors"
。我正在尝试建立一个“注册”页面来注册新用户。 这里是我的寄存器将数据从控制器传递到Codeigniter的视图时出错
function register(){
if($_POST){
$config = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'trim|required|min_length[3]|is_unique[users.username]'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required|min_length[5]'
),
array(
'field' => 'password2',
'label' => 'Password Confirm',
'rules' => 'trim|required|min_length[5]|matches[password]'
),
array(
'field' => 'user_type',
'label' => 'User Type',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|is_unique[users.email]|valid_email'
)
);
$this->load->library('form_validation');
$this->form_validation->set_rules($config);
if($this->form_validation->run() == FALSE){
$data['errors'] = validation_errors();
}else{
$data_array = array(
'username' => $_POST['username'],
'password' => sha1($_POST['password']),
'email' => $_POST['email'],
'user_type' => $_POST['user_type']
);
$this->load->model('user');
$userid = $this->user->create_user($data_array);
$this->session->set_userdata('userID', $userid);
$this->session->set_userdata('user_type',$_POST['user_type']);
redirect(base_url().'index.php/posts');
}
}
$this->load->helper('form');
$this->load->view('header');
$this->load->view('register_user');
$this->load->view('footer');
}
在上面当在验证我设置在$数据错误[“错误”]阵列的误差控制器。 下面
<h2>Register User</h2>
<?php if($errors): ?>
<div style="background:red;color:white;">
<?php echo $errors; ?>
</div>
<?php endif; ?>
的观点给予但是当我打开浏览器的注册页面,它显示了错误说"Undefined variable: errors"
。任何人都可以告诉我我做错了吗?
你需要传递一个数组到'$ this-> load-> view'作为第二个参数。 CodeIgniter从该数组中获取键,并使它们在视图文件中变量。确保'$ errors'在尝试回显之前已设置好。 –