Hi all okay so when i click on my home page on my login button it takes me to my login screen that all works well but when i submit the form it just reloads the page and does not redirect to the page specified in my controller. Also the error i want to display on the form does not work it gives me a parse error and i know that it is correct on the page.

Here is my controller

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Users extends CI_Controller
{

    public function login()
    {
        $data['error'] = 0;
        if ($_POST) {
            $this->load->model('user');
            $username = $this->input->post('username', true);
            $password = $this->input->post('password', true);
            $user = $this->user->login($username, $password);
            if (!$user) {
                $data['error'] = 1;
            } else {
                $this->session->set_userdata('userID', $user['userID']);
                $this->session->set_userdata('username', $user['username']);
                redirect(base_url().'site');
            }
        }
        $data['title'] = "Login";
        $data['content'] = 'login/login_view';
        $this->load->view('templates/login/template', $data);
    }

    public function forgot_password(){

    }


    function logout(){
        $this->session->sess_destroy();
        redirect(base_url().'login');
    }

}

Then my model contains the following code:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Model
{

    function create_user($data){
        $this->db->insert('users',$data);
    }

    function login($username,$password){
        $where = array(
            'username' => $username,
            'password' => sha1($password)
        );
        $this->db->select()->from('users')->where($where);
        $query = $this->db->get();
        return $query->first_row('array');
    }
}

Then my view is structured like this:

<section id="login-landing" class="block block-gray">
    <div class="container">
        <div class="card card-container">
            <a href="<?php echo base_url()?>">
                <img id="profile-img" class="profile-img-card" src="<?php echo base_url();?>assets/img/site/main.png"
                class="img-responsive">
            </a>
            <p id="profile-name" class="profile-name-card">Administrator Login</p>
            <?php if($error == 1){ ?>
                <p>The username / password did not match!</p>
            <? } ?>
            <form action="<?php echo base_url();?>users/login" method="post" class="form-signin">
                <input type="text" id="username" class="form-control" placeholder="Username" autofocus>
                <input type="password" id="password" class="form-control" placeholder="Password">
                <input type="submit" class="btn btn-lg btn-primary btn-block btn-signin" value="Sign In">
            </form><!-- /form -->
            <a href="<?php echo base_url();?>users/forgot_password" class="forgot-password"> Forgot your password</a>
        </div><!-- /card-container -->
    </div><!-- /container -->
</section>

Please help me understand where i am going wrong here, thanks in advance

Recommended Answers

All 3 Replies

Member Avatar for diafol

Haven't used CI in years so can't remember if array variables passed to a view are extracted or not.

You're passing $data['error'] and picking it up as $error in the view. Is this right? I can't remember.

Could it be that you're not creating a user - so that you don't get the redirect. The error should then show - but I mentioned $error already.

Haven't used CI in years so can't remember if array variables passed to a view are extracted or not. You're passing $data['error'] and picking it up as $error in the view. Is this right? I can't remember.

Hi! Yep, that's correct, as long that you feed the $data array to the view, then the keys will be extracted and used as variables. ;)

@Ventech_IT
Code seems fine. What about the parse error?

Member Avatar for diafol

Thanks c. Heh heh, too many frameworks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.