如何在CodeIgniter表单验证中使用Bootrstrap错误样式? [英] How to use Bootrstrap error styling within CodeIgniter form validation?

查看:83
本文介绍了如何在CodeIgniter表单验证中使用Bootrstrap错误样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有一点问题.我正在研究Bootstrap CSS.我对这个框架感到非常惊奇.这就是为什么我决定对此进行研究.我对CSS的了解并不多.但是我有点了解.

I have a little problem with my code. I am studying bootstrap CSS. And I am so amaze with this framework. That's why I decided to study this. I don't really don't have enough knowledge in CSS. But I understand a little.

我正在创建一个表单,如果用户名或密码错误,我希望收到一条验证消息.但是在创建我的验证并传递给模型之后.我不知道如何再次显示到同一页面并显示错误消息?

I am creating a form and I want to have a validation message if username or password are wrong.. I have a validation of required. But after creating my validation and passing to the model. I don't know how can I display again to the same page and display my error message?

这是我的简单代码.

在我的控制器中

class Admin extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('admin/adminauth');
    }

    public function index() {

        $data['title'] = "Administration";

        $this->form_validation->set_rules('username', 'Username', 'required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'required|xss_clean');

        if ($this->form_validation->run() == FALSE) {

            $this->load->view('header/header',$data);
            $this->load->view('contents/login',$data);
            $this->load->view('footer/footer');

        } else {

            $validate_login = $this->adminauth->validate_login();

            if($validate_login > 0) {



            } else {

                            //this should be go back again to the index and display my error message.
                $this->index();

            }

        }

    }

}   

我认为我有这个

<div class="panel-body form-admin-signin">

    <?php 
        $form_array = array(
            'class' => 'form-signin'
        );
        echo form_open('admin',$form_array); 
    ?>
        <div class="form-group">

            <h3 class="form-signin-heading">Username</h3>
                <input type="text" name="username" class="form-control inputs" placeholder="username" value="<?php echo set_value('username'); ?>"  />
                <?php echo form_error('username','<div class="error_msg">','</div>'); ?>
            <h3 class="form-signin-heading">Password</h3>
                <input type="password" name="password" class="form-control inputs" placeholder="password" value=""  />
                <?php echo form_error('password','<div class="error_msg">','</div>'); ?>
            <div class="checkbox">
                <label><input type="checkbox" /> Remember me</label>
            </div>
            <input type="submit" class="btn btn-primary login-btn" value="Login" />

        </div>
    <?php echo form_close(); ?>

    <?php   
            //THIS IS I WANT TO DISPLAY. IF INVALID THE CHECKING IN SQL IT WILL DISPLAY BELOW THE FORM
        $validate = '';
        if($validate == 1) {
    ?>
            <div class="alert alert-danger">
                <label>Invalid Username or Password please try again!</label>
            </div>
    <?php
        }
    ?>

</div>

在我的模特中,我有这个

In my model I have this

class AdminAuth extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    public function validate_login() {

        $username = $this->input->post('username');
        $password = md5($this->input->post('password'));

        $query = "SELECT username FROM user WHERE username = '".$username."' AND password = '".$password."' LIMIT 1";
        $get_result = $this->db->query($query);

        return $get_result->num_rows();

    }

}

就这些,希望您能帮助我.还是可以给我一个使用AJAX的示例吗?

That's all guys, I hope you can help me. Or can you give me an example using AJAX with this?

推荐答案

尝试

$this->session->set_flashdata('error', 'Error while sending enquiry email.');
$this->index();

或者您可以使用:-

$this->session->set_flashdata('error', 'Error while sending enquiry email.');
redirect('contoller');

查看时:-

<?php echo $this->session->flashdata('error');?>

这篇关于如何在CodeIgniter表单验证中使用Bootrstrap错误样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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