当我使用bootstrap Validator时,在Codeigniter 3.0.4中丢失POST数据

时间:2016-02-10 20:20:25

标签: php jquery twitter-bootstrap codeigniter

如果我在这里遗漏了一些东西,我会提前道歉。 我有一个简单的TEST控制器和视图。如果我不使用此脚本,表单将完美运行。

我没有使用AJAX或CSRF保护。

    <script>
        $(document).ready(function() {
            $('.vform').bootstrapValidator();
        });
    </script>

如果我使用上面的脚本,我会丢失POST数据。

这是我简单的TEST视图

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="<?=$description?>">
        <meta name="keywords" content="<?=$keywords?>">
        <meta name="author" content="xyz">
        <title><?=$title;?></title>
        <link href="<?=$canonical?>" rel="canonical">

        <link href="<?=base_url();?>assets/css/bootstrap.min.css" rel="stylesheet">
        <link href="<?=base_url();?>assets/css/app.css" rel="stylesheet">

        <link rel="shortcut icon" href="<?=base_url()?>favicon.ico" type="image/x-icon">
        <link rel="icon" href="<?=base_url()?>favicon.ico" type="image/x-icon">

        <!--[if lt IE 9]>
          <script src="<?=base_url();?>assets/js/shiv/html5shiv.min.js"></script>
          <script src="<?=base_url();?>assets/js/respond/respond.min.js"></script>
        <![endif]-->

    </head>
    <body>
        <div class="container-fluid">
            <header class="row">
                <div class="col-xs-12">
                    <h1>Test</h1>
                </div>
            </header>
            <div class="row">
                <div class="well col-xs-12 col-sm-9 center-block">
                    <?php
                        $att = array(
                            'class' => 'form-horizontal vform',
                            'name' => 'admin_login_form',
                            'id' => 'admin_login_form',
                        );
                        echo form_open(base_url().'test/login', $att);
                            echo form_fieldset('Admin Login');
                    ?>
                    <div class="form-group row">
                        <label for="email" class="col-xs-12 col-sm-2 control-label">Email</label>
                        <div class="col-xs-12 col-sm-6">
                            <div class="input-group">
                                <span class="input-group-addon">
                                    <span class="glyphicon glyphicon-user"></span>
                                </span>
                                <input type="text" class="form-control" name="email" id="email" placeholder="Email" maxlength="100" data-bv-notempty="true" data-bv-notempty-message="Please enter your email" />
                            </div>
                        </div>
                        <div class="col-xs-12 col-sm-4" id="emailmsg">

                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="password" class="col-xs-12 col-sm-2 control-label">Password</label>
                        <div class="col-xs-12 col-sm-6">
                            <div class="input-group">
                                <span class="input-group-addon">
                                    <span class="glyphicon glyphicon-qrcode"></span>
                                </span>
                                <input type="password" class="form-control" name="password" id="password" placeholder="Password" maxlength="50" />
                            </div>
                        </div>
                        <div class="col-xs-12 col-sm-4" id="passwordmsg">

                        </div>
                    </div>

                    <div class="form-group row">
                        <div class="col-xs-12 col-sm-offset-2 col-sm-6">
                            <input type="submit" name="login_sub" id="login_sub" class="btn btn-primary" />
                        </div>
                        <div class="col-xs-12 col-sm-4" id="loginmsg">

                        </div>
                    </div>
                    <?php
                            echo form_fieldset_close();
                        echo form_close();
                    ?>
                </div>
            </div>
            <footer class="row">
                <div class="col-xs-12">
                    <p>&copy;Test</p>
                </div>
            </footer>
        </div>

        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="<?=base_url();?>assets/js/jquery-1.12.0.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="<?=base_url();?>assets/js/bootstrap.min.js"></script>
        <!-- Validator -->
        <script src="<?=base_url();?>assets/js/bsvalid/bootstrapValidator.min.js"></script>
        <script>
            $(document).ready(function() {
                $('.vform').bootstrapValidator();
            });
        </script>
    </body>
</html>

这是我的简单测试控制器

    <?php
        class Test extends CI_Controller {

            public function login () {
                $this->load->helper('url');
                $data = array();
                $data['description'] = 'Admin page - Test';
                $data['keywords'] = 'Test, admin, login';
                $data['title'] = 'Test - Admin - Login';
                $data['canonical'] = 'test/one';

                $this->load->library('form_validation');
                //Form validation rules
                $this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[3]|max_length[50]|valid_email');
                $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]|max_length[20]');

                //Form Submission
                if ($this->input->post('login_sub')) {
                    echo "jjjj";
                    var_dump($_POST);
                    if ($this->form_validation->run() !== FALSE) {
                        echo 'All is well';
                    }

                }

                $this->load->view('test/one', $data);
            }

我尝试使用版本0.4.9和0.5 Bootstrap Validator。 如果我删除验证器jquery,我将获得POST输出。但是当我包含验证器jquery段时,页面似乎在提交时刷新并且没有输出。 老实说我很无能,因为我没有使用任何AJAX或CSRF。纯客户端jquery函数如何干扰POST数据? 附:我也尝试过使用bootstrap验证器jquery函数而不是内联data-bv验证规则。 P.P.S.相同的代码在codeigniter 3.0.3上完美运行...我倾向于认为这是一个codeigniter 3.0.4问题。 如果有人可以指导我,我将不得不这样做。

1 个答案:

答案 0 :(得分:0)

首先检查你是否设置了base_url它不是必需的,但有时如果你使用的codeIgniter 3版本不能提交正确的

的config.php

$config['base_url'] = 'http://localhost/your_project_name/';

测试控制器

文件名:Test.php 检查首字母大写

<?php

class Test extends CI_Controller {

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

    public function login () {

        $data['description'] = 'Admin page - Test';
        $data['keywords'] = 'Test, admin, login';
        $data['title'] = 'Test - Admin - Login';
        $data['canonical'] = 'test/one';

        $this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[3]|max_length[50]|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]|max_length[20]');

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

             $this->load->view('test/one', $data);

        } else {

            var_dump($this->input->post());

            echo 'All is well';

        }

    }

}

侧面标签中的jQuery位置效果更好。但仍然将bootstrap js留在底部。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="<?php echo $description ;?>">
<meta name="keywords" content="<?php echo $keywords;?>">
<meta name="author" content="xyz">
<title><?php echo $title;?></title>
<link href="<?php echo $canonical;?>" rel="canonical">

<script src="<?php echo base_url('assets/js/bsvalid/bootstrapValidator.min.js');?>"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="<?php echo base_url('assets/js/jquery-1.12.0.min.js');?>"></script>
</head>

<body>

<?php

$att = array(
    'class' => 'form-horizontal vform',
    'name' => 'admin_login_form',
    'id' => 'admin_login_form',
);

echo form_open('test/login', $att);

?>


// Form Content Here


<?php echo form_close();?>

<script src="<?php echo base_url('assets/js/bootstrap.min.js');?>"></script>

<script>
$(document).ready(function() {
   $('#admin_login_form').bootstrapValidator();
});
</script>

</body>

</html>