Welcome Guest, Not a member yet? Register   Sign In
Problem with flashdata
#1

[eluser]Joures[/eluser]
I want to display an error message if a user has
entered the wrong username or password in a login system.
I made use of flashdata and i kinda got it to work except it's not displayin
the error on the first click off the submit button, but on the second. And then it
stays there until i've refreshed the page twice. Any ideas?

My code looks like this:

Model
Code:
class User extends Model {
    
    function validate() {
        
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('users');
        
        if($query->num_rows == 1) {
            
            return true;
        }
    }

Controller
Code:
class Login extends Controller {
    
    function index() {
        
        $this->load->view('misc/login_form');
    }
    
    function validate_credentials() {
        
        $query = $this->user->validate();
        
        if($query) {
            
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
            
            $this->session->set_userdata($data);
            redirect('dashboard/index');
            
        } else {
            
            $message = '<div class="flash_error">Fel användarnamn eller lösenord.</div>';
            $this->session->set_flashdata('message', $message);
            
            $this->index();
        }
    }

View
Code:
<div class="box-content-25">
                &lt;?php echo $this->session->flashdata('message'); ?&gt;
                
                &lt;?= form_open('login/validate_credentials'); ?&gt;
                
                <p>&lt;?= form_input('username', set_value('username', 'Användarnamn')); ?&gt;</p>
                <p>&lt;?= form_password('password', 'lösenord'); ?&gt;</p>

                &lt;?= form_submit('submit', 'Logga in'); ?&gt;
            </div>
#2

[eluser]smilie[/eluser]
I had very same problem and could not solve it.
This is how I did it though...

Model is same (no changes).

Controller:
Code:
class Login extends Controller {
    
    function index($extra='') {
        if(trim($extra) != '')
        {
            $this->load->view('misc/login_form',$extra);
        }
        else
        {
            $this->load->view('misc/login_form');
        }
    }
    
    function validate_credentials() {
        
        $query = $this->user->validate();
        
        if($query) {
            
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
            
            $this->session->set_userdata($data);
            redirect('dashboard/index');
            
        } else {
            
            # $message = '<div class="flash_error">Fel användarnamn eller lösenord.</div>';
            # $this->session->set_flashdata('message', $message);
            # Instead:
            $extra['info'] = 'error';
            $extra['msg'] = 'Log in is not correct.';
            
            $this->index($extra);
        }
    }

And then in view:
Code:
<div class="box-content-25">
                &lt;?php
                if($info != '')
                {
                   <div class='&lt;?=$info;?&gt;'>
                       &lt;?=$msg;?&gt;
                   </div>
                 }?&gt;
                
                &lt;?= form_open('login/validate_credentials'); ?&gt;
                
                <p>&lt;?= form_input('username', set_value('username', 'Användarnamn')); ?&gt;</p>
                <p>&lt;?= form_password('password', 'lösenord'); ?&gt;</p>

                &lt;?= form_submit('submit', 'Logga in'); ?&gt;
            </div>

And just to make it complete here is CSS for the class error / warning etc...:
Code:
/*
DIV holders for info, success, warning, error and validation messages
*/
.info, .success, .warning, .error, .validation {
    border: 1px solid;
    margin: 10px 0px;
    padding:15px 10px 15px 50px;
    background-repeat: no-repeat;
    background-position: 10px center;
}
.info {
    color: #00529B;
    background-color: #BDE5F8;
    background-image: url('../images/info.png');
}
.success {
    color: #4F8A10;
    background-color: #DFF2BF;
    background-image:url('../images/success.png');
}
.warning {
    color: #9F6000;
    background-color: #FEEFB3;
    background-image: url('../images/warning.png');
}
.error {
    color: #D8000C;
    background-color: #FFBABA;
    background-image: url('../images/error.png');
}

Images are attached...
Of course, change style / images to your needs :-)
#3

[eluser]Joures[/eluser]
That worked wonders! Thank you Big Grin
#4

[eluser]smilie[/eluser]
Sure, no problem :-)

Have fun!

Cheers,
Smilie




Theme © iAndrew 2016 - Forum software by © MyBB