首页 > 解决方案 > 在codeigniter中使用form_error函数后,html消失

问题描述

我正在尝试在我的 html 表单中添加表单错误消息。问题存在于第一个视图中。在用户函数中,html 从我开始使用的地方消失form_error()。我的原始表单设计是这样的:我的原始表单 ,但在form_error第一个输入下添加后:我的表单错误图像 这是我的 html 代码

<div class="form-group">
     <label for="name"><i class="zmdi zmdi-account material-icons-name"></i></label>
     <input type="text" name="username" id="name"  placeholder="Your Name" />
     <?php echo form_error('username','<p class="p_errror" id="name_error_p">','</p>') ?>
</div>
<div class="form-group">
     <label for="email"><i class="zmdi zmdi-email"></i></label>
     <input type="text" name="email" id="email" placeholder="Your Email"/>

     <?php echo form_error('email','<p class="p_errror" id="name_error_p">','</p>') ?>
</div>

这是我的控制器

<?php 
    class Register extends CI_Controller
    {
    public function user()
    {
        $this->load->view('files/registration/signup');
    }
    public function login()
    {
        $this->load->view('files/registration/login');
    }
    public function description()
    {
        $this->load->view('files/registration/description');
    }
    public function registerMe()
    {

            $this->load->helper('form','url');
            $this->load->library('form_validation');
            $this->form_validation->set_rules('username', 'Username', 'required|alpha|max_length[15]');
            $this->form_validation->set_rules('pass', 'Password', 'required',
                            array('required' => 'You must provide a %s.')
                    );
             $this->form_validation->set_rules('re_pass', 'Confirm Password', 'required|matches[pass]');

             $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
                    if ($this->form_validation->run()===FALSE) {
                          $this->load->view('files/registration/signup');

                    }
                    else
                    {
                        $this->load->model('RegisterUser');
                        $this->RegisterUser->registerme($_POST);
                    }
    }
    }
    ?>

标签: phpcodeigniter

解决方案


您还需要在注册功能中加载表单助手。 form_error是 form_helper 中的一个函数。(这对于具有表单视图的其他功能也是必需的)

public function user()
{
    $this->load->helper('form');
    $this->load->view('files/registration/signup');
}

html 消失,因为有 php 错误。发生这种情况时检查您的错误日志或将 codeigniter 环境更改为 index.php 中的开发


推荐阅读