Codeigniter $this->load->view 到页面的特定部分(数据切换 - 选项卡)

Codeigniter $this->load->view to a Particular Section of page (Data-Toggle - Tab)

我有这个管理面板,我在其中使用了不同的数据切换选项卡。

在 CI(3) 中,如果我使用 redirect('user/dashboard#new'); ,它会将我重定向到更正视图部分但不会出现 form_validation 错误。

如果我尝试 $this->dashboard('user/dashboard#new'); 它会呈现错误,但会将我带到错误的页面部分(不在#new)。

我刚刚开始使用 CI 进行开发,并寻求前辈的帮助。

提前致谢。

控制器(用户)

public function dashboard() {
if($this->session->userdata('is_logged_in')){
$data['homepage'] = '../../templates/vacations/users/dashboard';
$this->load->view('template_users',$data);
}else{
$data['session_error']='Either the session has expired or you have tried to access this page directly';
$this->load->view('../../templates/vacations/headfoot/header-login');
$this->load->view('../../templates/vacations/users/session-error', $data);
$this->load->view('../../templates/vacations/headfoot/footer-login');
}}

表单验证

if($this->form_validation->run() == FALSE)
{
$this->dashboard('user/dashboard#new');
} else {
$this->load->model('model_users');
if($query = $this->model_users->insert_property_details())
{
redirect('user/dashboard#new'); 
} else {
redirect('user/dashboard#new');
}}}

$this->dashboard('user/dashboard#new'); 只是 runs/calls 当前页面中的方法。 'user/dashboard#new' 什么都不做,因为该方法不是为了接受参数而编写的:

public function dashboard(/* arguments would be here normally */) { ... }

在 运行 验证后立即重定向将不起作用,因为您将在加载新页面时丢失验证错误。

您需要将错误保存在某个地方,例如会话数据,然后重定向到仪表板,然后从保存的位置加载错误并将它们显示在仪表板视图上。

这是一个使用会话数据的示例。

表格方法:

if($this->form_validation->run() == FALSE)
{
    $this->session->set_userdata('validation_errors', validation_errors());
    $this->session->mark_as_flash('validation_errors'); // data will automatically delete themselves after redirect
    redirect('user/dashboard#new');
}
else { ... }

仪表板方法:

public function dashboard() 
{
    if($this->session->userdata('is_logged_in')){
        $data['validation_errors'] = $this->session->userdata('validation_errors');
        $data['homepage'] = '../../templates/vacations/users/dashboard';
        $this->load->view('template_users',$data);
    } else { ... }
}

从表单验证中获取错误数组class(对于下面的评论):

class MY_Form_validation extends CI_Form_validation {
    public function error_array()
    {
        return $this->_error_array;
    }
}