codeigniter jQuery的AJAX:POST数据到控制器问题 [英] Codeigniter jquery ajax: post data to controller issues

查看:166
本文介绍了codeigniter jQuery的AJAX:POST数据到控制器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在登录系统codeigniter 3.0和jQuery阿贾克斯。我要发布的数据控制器上使用Ajax和返回值,如果抛出AJAX中的数据是从MySQL数据库匹配。我也尝试那些<一href="http://stackoverflow.com/questions/18529070/$c$cigniter-ajax-send-data-to-controller-using-ajax-$c$c">suggestion/s从这个现有的帖子但我认为它不工作。我不想依靠从codeigniter重定向。

I'm working on login system in codeigniter 3.0 and jquery ajax. I want to post data to controller using ajax and return a value if the data thrown by ajax is match from the mysql database. I also try those suggestion/s from this existing post but i think its not working. I don't want to rely the redirect from codeigniter.

主要问题:我要检查,如果用户名和密码的数据是不正确的,它会提示一个错误,没有页面刷新。但我确实是抛出一个布尔值从控制器查看投影错误,如HTML标签。

MAIN PROBLEM: i want to check if the username and password data are incorrect, it prompts an error with no page refresh. But i did was throw an boolean value from controller to view for projecting an error-like html tag.

继承人的code:

readyLogin.js

var ReadyLogin = function () {

return {
    init: function () {
        /*
         *  Jquery Validation, Check out more examples and documentation at https://github.com/jzaefferer/jquery-validation
         */

        /* Login form - Initialize Validation */
        $('#form-login').validate({
            errorClass: 'help-block shake animated', // You can change the animation class for a 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
                },
                'login-password': {
                    required: true,
                    minlength: 5
                }
            },
            messages: {
                'login-username': {
                    required: "Please enter Administrator's username."
                },
                'login-password': {
                    required: "Please provide Administrator's password.",
                    minlength: 'Your password must be at least 5 characters long.'
                }
            }
        });
        $("#form-login").submit(function(){
            $.ajax({
                type: "POST",
                url: $('#form-login').attr('action'),
                data: {"login-username": $("#login-username").val(), "login-password": $("#login-password").val()},
                dataType: "json",
                success: function (returnData) {
                    alert(data); // to check if there's a thrown data from the view to controller
                    // below is a "trial" code that checks the status if the controller receives data from ajax
                    if (returnData.status == true) {
                        console.log(returnData.status);
                    } else {
                        console.log('empty');
                    }
                }
            });


        });
    }
};
}();

控制器/ login.php中

<?php

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

class Login extends CI_Controller {

public function __construct() {
    parent::__construct();
    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->helper('html');
}

public function index() {
    $data['error'] = false;
    $result = $data['error'];
    if ($_POST) {
        $this->load->model('Login_model');
        $username = $this->input->post('login-username', true);
        $password = $this->input->post('login-password', true);
        $user = $this->Login_model->login($username, $password);
        if (!$user && $user == null) {
            $data['error'] = true; //throwing boolean value to make error html tag
            $result = array('status' => true);
            json_encode($result);
        } else {    
            $data['error'] = false;
            $this->session->set_userdata('username', $user['usename']);
            $this->session->set_userdata('password', $user['password']);
            redirect(base_url() . 'Maps');
        }
    }
    $this->load->view('Backend/page_ready_login', $data);
}

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

}

型号/ login.php中

<?php

class Login_model extends CI_Model {

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

}

的意见/后端/ page_ready_login.php

<?php
include 'assets/Backend/inc/config.php';
$template['title'] = 'BCGIS | LOGIN';
$template['page_preloader'] = true;
?>
<?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(); ?>" method="post" class="form-horizontal">
        <?php if ($error == true) { ?>
            <h5 class="alert alert-danger shake animated" id="login-error">Your Username/Password is incorrect!</h5>
        <?php } ?>
        <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" id="login-button" name="login-button" 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> &copy; <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 and execute javascript code used only in this 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'; ?>

任何建议/ s和帮助修复,这是很多AP preciated。谢谢你。

Any suggestion/s and help fixing this is much appreciated. Thanks.

推荐答案

在你的控制器的方法,改变这样的:

In your controller method, change this:

if (!$user && $user == null) {

要这样:

if (!$user || $user == null) {

这篇关于codeigniter jQuery的AJAX:POST数据到控制器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆