使用jquery、ajax和codeigniter验证Boostrap登录弹出窗口模型

Boostrap login popup model validation using jquery ajax and codeigniter

本文关键字:登录 窗口 模型 Boostrap 验证 jquery ajax codeigniter 使用      更新时间:2023-09-26

我的问题:我点击登录按钮并打开登录弹出模型,我插入用户名和密码并按提交按钮,当用户名和密码正确时,则重定向仪表板页面,如果用户名和密码错误,则在弹出模型中显示错误消息。我使用codeigniter框架。请帮帮我……

控制器:

class Property extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('session');
    }
    public function index() {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        if ($username == 'admin' && $password == 'admin') {
            $user = array(
                'username' => $username,
                'password' => $password
            );
            $this->session->set_userdata($user);
            echo json_encode($user);
        } else {
            $errormsg = "Invalid UserName and Password";
            echo json_encode($errormsg);
        }
    }
    public function property_owner_detail() {
        $username = $this->session->userdata('username');
        if (!empty($username)) {
            //$this->output->cache(3600);
            $this->load->model('event_model');
            $this->load->helper('date');
            $data = array(
                'page_title' => 'Property',
                'location_collection' => $this->event_model->list_all()
            );
            $this->load->view('property/index', $data);
        } else {
            redirect('/Site');
        }
    }
}

视图:

<li><a href="#" style="color: red" data-toggle="modal" data-target="#property_owner_login">Login</a></li>

<div class="modal fade" id="property_owner_login" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title text-primary">Login</h4>
            </div>
            <div class="modal-body">
                <?php echo form_open(); ?>
                <div class="form-group">
                    <label class="control-label">Username:</label>
                    <input type="text" class="form-control" name="username" id="username" required="">
                </div>
                <div class="form-group">
                    <label class="control-label">Password:</label>
                    <input type="password" class="form-control" name="password" id="password" required="">
                </div>
                <?php echo form_close(); ?>
            </div>
            <div class="modal-footer">
                <button type="button" name="submit" id="property_owner_login" class="btn btn-success">Login</button>
            </div>
        </div>
    </div>
</div>
Javascript文件

var property_owner_login = {
    mode: "Add",
    modal: $("#property_owner_login"),
    add: function () {
        var username = $("#username").val();
        var password = $("#password").val();
        $.ajax({
            url: "/Property",
            type: 'POST',
            data: {username: username, password: password},
            success: function (data) {
                console.log(data)
                var obj = JSON.parse(data);
                //console.log(data.username);
                if (obj.username == 'admin' && obj.password == 'admin') {
                    window.location.href = 'property/property_owner_detail';
                } else {
                }
            }
        });
    }
};
$(document).ready(function () {
    $("#property_owner_login").click(function () {
        if (property_owner_login.mode == "Add") {
            property_owner_login.add();
        }
    });
});

在javascript函数中,您需要检查响应是否有错误消息。你可以按照你喜欢的方式设置格式,但对于一个登录,你不想泄露太多信息,比如"电子邮件未被识别",因为我可以继续拖延,直到电子邮件被识别,然后我就在那里了。所以只是有一个一般的"电子邮件和密码不匹配,请再试一次。"(除非你有其他机制,比如强制延迟2、4、8、16、32秒等,或者3次尝试和锁定等)。

你必须检查你的js响应,如果它是,说,等于ERROR或FAIL或FALSE或无论你想要这样做,然后将通用的错误信息插入到模态内的特定(尽管最初是空白的)错误div中,样式无论你想要它(使用$('#myErroDiv') = html('错误信息')或类似),就像你检查它们是否管理的方式一样。

您对登录尝试的响应可以是一个简单的TRUE或FALSE, TRUE为重定向,FALSE为一般错误消息。他们在TRUE上访问的用户面板可以根据他们的角色来处理是否显示管理页面。在尝试登录时不需要这样做。

希望有帮助,

保罗。