代码之家  ›  专栏  ›  技术社区  ›  Sumit Bhatia

Codeigniter验证无效,页面正在重定向到空白页面

  •  2
  • Sumit Bhatia  · 技术社区  · 8 年前

    我是codeigniter的新手。这两个页面的位置都是正确的。我想有一些代码可以激活视图页面的form_validator。

    编辑:我添加了

              if ($this->form_validation->run()){
    $this->load->view('home');
        }else{
      $this->load->view('login');
          }  
    

    控制器中的上述代码仍然将页面重定向到空白页面

       <?php  
    
     class Welcome extends CI_Controller {
      function __construct()
        {
            parent:: __construct();
            $this->load->helper(array('form', 'url', 'html'));
            $this->load->library('form_validation');               
             $this->load->database();
    
        }
    public function index()
    {       
        $this->form_validation->set_rules('username','username', 'required|min_length[5]|max_length[15]');
        $this->form_validation->set_rules('email','email', 'trim|required|valid_email');
    
    
             $this->form_validation->run();
        $this->load->view('login');
     {
    
    
    
       }
       }
       ?>
    

    这是查看页面。我也使用了纯html表单标记而不是form_open方法,但它也不起作用,它将我重定向到同一个页面,并没有显示任何错误

                <!DOCTYPE html>
                 <html lang="en">
                 <head>
                        <meta charset="utf-8">
                          <title>Welcome to CodeIgniter</title>
                           </head>
                            <body>
                            <div id="container">
    <?php echo validation_errors('form'); ?>
    <?php echo form_open('form'); ?>
    
     <?php echo form_error('username');  ?>
     <input type='text' name ='username' >
     <?php echo form_error('email');  ?>
     <input type='email' name='email'>
    
     <input type="submit" value='go'>
      </form>
      </div>
    
          </body>
         </html>
    
    3 回复  |  直到 8 年前
        1
  •  0
  •   Community Egal    7 年前

    试试这个

    在视图中 Link to Comment

    <?php echo form_open('welcome'); ?> 
    

    在控制器中

    public function index()
    {       
        $this->form_validation->set_rules('username','username', 'required|min_length[5]|max_length[15]');
        $this->form_validation->set_rules('email','email', 'trim|required|valid_email');
    
        if ($this->form_validation->run() == FALSE)
        {
            # Fail
            $this->load->view('login');
        }
        else
        {
            # Success
            $this->load->view('home'); # Load your contrller 
        }
    }
    

    Check this Tutorial as well

        2
  •  0
  •   Devidas Kadam    8 年前

    使用此

    if ($this->form_validation->run()){
        $this->load->view('home');
    }else{
          $this->load->view('login');
    }
    
        3
  •  0
  •   Devidas Kadam    8 年前

    将索引函数的完整体更改为如下所示

    if (isset($_POST)) {
        $this->form_validation->set_rules('username','username','required|min_length[5]|max_length[15]');
        $this->form_validation->set_rules('email','email','trim|required|valid_email');
    
        if($this->form_validation->run()){
            $this->load->view('home');
        }else{
            $this->load->view('login');
        }
    } else {
        $this->load->view('login');
    }