Cannot get flash data to work to a page after register

删除回忆录丶 提交于 2019-12-12 16:09:41

问题


So I am trying to get from login screen to admin screen, and I set flash data so I can tell the users if they are logged in, and also check if there is more than one user or a wrong password it will show sorry you are not logged in. So this is my controller.

public function insertInformation(){
                $this->load->helper('form');
                $this->load->library('form_validation');

                $this->form_validation->set_rules('name', 'Name', 'required');
                $this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_check_if_email_exists');
                $this->form_validation->set_rules('phone', 'Phone', 'required');
                $this->form_validation->set_rules('password', 'Password', 'required|min_length[4]|max_length[32]');
                $this->form_validation->set_rules('password_confirm', 'Password Confirm', 'required|matches[password]');


                if ($this->form_validation->run() == FALSE){
                $this->load->view('header_view');
                $this->load->view('login_view');
                $this->load->view('footer_view');
                }else{

                    $data =  array(
                    'name' => $this->input->post('name'),
                    'email' => $this->input->post('email'),
                    'phone' => $this->input->post('phone'),
                    'password' => $this->input->post('password')

                );


                    $this->load->model('main_page');
                  $user_id = $this->main_page->storeRegisterInfo($data);

                    if($user_id){
                        $user_data = array(
                            'user_id' =>$user_id,
                            'name'=> $name,
                            'logged_in' => true
                        );

                        $this->session->set_userdata($user_data);
                        $this->session->set_flashdata('login_sucess','you are now loggedin');
                        $this->Admin();
                    }else{
                        $this->session->set_flashdata('login_failed','you are not loggedin');
                        $this->login();
                    }

                }




        }

This is the modal where the data is being inserted and I am not sure how to check if the user id exist and as well as check if the user has valid info to login.

public function storeRegisterInfo($data){

            $insert = $this->db->insert('new_users',$data);
            $result = $this->db->get('new_users');
            if($result ->num_rows()== 1){
                return $result->row(0)->id;
            }else{
                return false;
            }


        }

And lastly for the view in login function its like this. Basically this is a register form but i wrote login everywhere by mistake so please dont get confused.

<p class="bg-danger">

<?php if($this->session->flashdata('login_failed')): ?>
<?php echo $this->session->flashdata('login_failed'); ?>

<?php endif; ?>


</p>

And similarly for the admin.

<p class="bg-success">

<?php if($this->session->flashdata('login_success')): ?>
<?php echo $this->session->flashdata('login_success'); ?>

<?php endif; ?>


</p>


<h1>hello</h1>

So I cannot get the flash data to work ,and as well as cant identify if the user already exists or wrote the wrong password.


回答1:


After setting flash data redirect the page

redirect('controllerNmae/Method');

EDIT 01

In view

<?php
    if(!empty($this->session->flashdata('login_sucess')))
    {
        echo $this->session->flashdata('login_sucess');
    }

    if(!empty($this->session->flashdata('login_failed')))
    {
        echo $this->session->flashdata('login_failed');
    }
?>

or

<?php
    if(!empty($this->session->flashdata('login_sucess')))
    {
        ?>
        <script>
            alert('<?php $this->session->flashdata('login_sucess'); ?>');
        </script>
    <?
    }

     if(!empty($this->session->flashdata('login_failed')))
    {
        ?>
        <script>
            alert('<?php $this->session->flashdata('login_failed'); ?>');
        </script>
    <?
    }
?>

Tip: Use this library alertify JS. Its gives nice user experience
Ex

<script>
   alertify.success('<?php $this->session->flashdata('login_sucess'); ?>');
</script>


来源:https://stackoverflow.com/questions/35138656/cannot-get-flash-data-to-work-to-a-page-after-register

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!