简体   繁体   English

CodeIgniter控制器-将变量传递给index()

[英]CodeIgniter Controller - pass variable to index()

still very new to CodeIgniter and trying to do the following. 对于CodeIgniter还是很新的,并尝试执行以下操作。 I may be doing this wrong, and any direction, greatly appreciated. 我可能做错了任何方向,非常感谢。

I have a function that is called on submit of login form, validate_creds . 我有一个在提交登录表单validate_creds调用的函数。 If successful the form logs in, if not successful I want to set an $error_msg and reload the form. 如果成功登录,如果不成功,我要设置$error_msg并重新加载表单。 The validate_creds() appears to validate correctly but as you can see on the else statement I want to reload the index() passing the $error_msg displaying the message at the top of the form. validate_creds()似乎可以正确验证,但是正如您在else语句上看到的那样,我想重新加载index()并传递$error_msg ,在表格顶部显示消息。

public function index($error_msg = '')
        {
            //data
            $data['login'] = "Logged In";

            if($error_msg !== '') {
                $data['error_msg'] = $error_msg;
            }

            //view
            $this->load->view('templates/login/login_header',$data);
            $this->load->view('login/login_form', $data);
            $this->load->view('templates/login/login_footer',$data);
        }


         public function validate_creds()
                    {
                        $this->load->model('user_model');
                        $query = $this->user_model->validate();
                        if($query)//if the users creds have been validated...
                        {
                        $data = array(
                            'username' => $this->input->post('username'),
                            'is_logged_in' => true
                        );

                        $this->session->set_userdata($data);
                        redirect('gallery');
                    }

                    else
                    {
                        $error_msg = "Your username or password is not correct.";
                        $this->index($error_msg);
                    }
                }

Form Message 表格讯息

<?php if($error_msg != ''): ?>
  <div class="alert alert-error">
    <?php echo $error_msg; ?>
  </div>
<?php endif; ?>

What currently is happening is the $error_msg is displaying all the time. 当前正在发生的是$error_msg一直显示。

I hope this is just how it came out with copy pasting because this code is hard to read. 我希望这就是复制粘贴的结果,因为此代码很难阅读。 check out php codesniffer and as an industry standard, check out some documentation on zend about php code format. 查看php codeniffer,并作为行业标准,查看zend上有关php代码格式的一些文档。

so your logic requires the error message be injected into the index function (which I'm assuming is your action but just not labeled as so), and if it's not injected, it doesn't declare the error message because it's not injected. 因此,您的逻辑要求将错误消息注入到索引函数中(我假设这是您的操作,但未标记为原来的行为),并且如果未注入错误消息,则不会声明错误消息,因为未注入错误消息。

i would look at $this->user_model->validate(); 我会看$ this-> user_model-> validate(); to ensure that's properly handling the http request (post). 以确保正确处理了http请求(发布)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM