javascript - codeigniter ajax login error div and session issue -
i have stuck creating login using codeigniter 3.0 appui 2.1 bootstrap theme, show error div in login , everytime logged in, direct maps view when navigate information table view , go maps view, redirect login view.
any suggestions? here's code:
model/login_model.php
class login_model extends ci_model { public function login($username, $password) { $passwordencrypt = sha1($password); $this->db->where('username', $username); $this->db->where('password', $passwordencrypt); $query = $this->db->get('admin'); if ($query->num_rows() == 1) { foreach ($query->result() $row) { $data = array( 'username' => $row->username, 'password' => $row->password, 'logged_in' => true ); } $this->load->library('session'); $this->session->set_userdata($data); return true; } else { return false; } } public function isloggedin() { header("cache-control: no-store, no-cache, must-revalidate"); header("cache-control: post-check=0, pre-check=0", false); header("pragma: no-cache"); $is_logged_in = $this->session->userdata('logged_in'); if (!isset($is_logged_in) || $is_logged_in !== true) { redirect('/'); exit; } }
}
views/page_ready_login.php
<?php include 'assets/backend/inc/config.php'; $template['title'] = 'bcgis | login'; ?> <?php include 'assets/backend/inc/template_start.php'; ?> <!-- login container --> <div id="login-container"> <!-- login header --> <h1 class="h3 text-light text-center push-top-bottom fadein animated"> <div class="row"> <div class="col-lg-3 fadein animated"> <img src="<?= base_url(); ?>assets/backend/images/ph_seal_davao_del_norte_panabo_city.png" style="width: 95px; height: 80px;"/> </div> <div class="col-lg-9 fadein animated"> <strong>brgy. cagangohan geographical information system</strong> </div> </div> </h1> <!-- end login header --> <!-- login block --> <div class="block fadein animated"> <!-- login title --> <div class="block-title"> <h2>administration login</h2> </div> <!-- end login title --> <!-- login form --> <form id="form-login" action="<?= base_url(); ?>maps" method="post" class="form-horizontal"> <p class="alert alert-danger" id="response"><b>invalid administrator's username or password.</b></p> <div class="form-group"> <div class="col-xs-12"> <input type="text" id="login-username" name="login-username" class="form-control" placeholder="username.."> </div> </div> <div class="form-group"> <div class="col-xs-12"> <input type="password" id="login-password" name="login-password" class="form-control" placeholder="password.."> </div> </div> <div class="form-group form-actions"> <div class="col-sm-offset-2 col-sm-8 text-center"> <center> <button type="submit" class="btn btn-effect-ripple btn-block btn-primary"><i class="fa fa-check"></i> sign in</button> </center> </div> </div> </form> <!-- end login form --> </div> <!-- end login block --> <!-- footer --> <footer class="text-muted text-center fadein animated"> <small><span id="year-copy"></span> © <a href="#"><?php echo $template['name'] . ' ' . $template['version']; ?></a></small> </footer> <!-- end footer --> </div> <!-- end login container --> <?php include 'assets/backend/inc/template_scripts.php'; ?> <!-- load , execute javascript code used in page --> <script src="<?= base_url(); ?>assets/backend/js/pages/readylogin.js"></script> <script> $(function () { readylogin.init(); }); </script> <?php include 'assets/backend/inc/template_end.php'; ?>
controller/login.php
class login extends ci_controller { public function __construct() { parent::__construct(); $this->load->library('session'); $this->load->helper('form'); $this->load->helper('url'); $this->load->helper('html'); $this->load->library('form_validation'); $this->load->model("login_model"); } public function index() { $this->load->view('backend/page_ready_login'); } public function logout() { $this->session->sess_destroy(); redirect(base_url(), 'refresh'); } public function login() { }
controller/maps.php (where suppose direct when logged in)
class maps extends ci_controller { public function __construct() { parent::__construct(); $this->load->model('login_model'); } public function index() { $username = $this->input->post('login-username'); $password = $this->input->post('login-password'); if ($this->login_model->login($username, $password)) { $this->login_model->isloggedin(); $this->load->view('backend/page_comp_maps'); } else { redirect(base_url(), 'refresh'); } }
assets/backend/js/pages/readylogin.js
var readylogin = function () { return { init: function () { /* * jquery validation, check out more examples , documentation @ https://github.com/jzaefferer/jquery-validation */ /* login form - initialize validation */ $('#form-login').validate({ errorclass: 'help-block shake animated', // can change animation class different entrance animation - check animations page errorelement: 'div', errorplacement: function (error, e) { e.parents('.form-group > div').append(error); }, highlight: function (e) { $(e).closest('.form-group').removeclass('has-success has-error').addclass('has-error'); $(e).closest('.help-block').remove(); }, success: function (e) { e.closest('.form-group').removeclass('has-success has-error'); e.closest('.help-block').remove(); }, rules: { 'login-username': { required: true, error: true }, 'login-password': { required: true, minlength: 5, error: true } }, messages: { 'login-username': { required: "please enter administrator's username.", error: "invalid administrator's username." }, 'login-password': { required: "please provide administrator's password.", minlength: "admin's password must @ least 5 characters long.", error: "invalid administrator's password." } } }); $("#form-login").submit(function (e){ e.preventdefault(); var url = $(this).attr('action'); var method = $(this).attr('method'); var data = $(this).serialize(); $.ajax({ url:url, type:method, data:data }).done(function(data){ if(data !=='') { $("#response").show('fast'); $("#response").effect( "shake" ); $('#form-login')[0].reset(); } else { window.location.href = url; throw new error('go'); } }); }); $( "div" ).each(function( index ) { var cl = $(this).attr('class'); if(cl ==='') { $(this).hide(); } }); } };
}();
in readylogin.js between $('#form-login').submit(function(e){}), seems code in didn't work.
for more, here link project.
any appreciated. :)
Comments
Post a Comment