与codeigniter的表单验证问题

I'm trying to develop a simple login system, but i'm facing issue during data validation.

Everything works if I enter valid data.

but when i enter invalid data it like empty user or password, it gives me validation erorr, but after that if i enter the correct user data. it wont work, it will just reload the the same page. The strange thing that i noticed is that the url on the address bar keeps getting longer.

My login url after clicking submit button the first time.

http://localhost/ci/login/validate

My login url after clicking submit button the second time.

http://localhost/ci/login/login/validate

Every time i click the submit button, it keeps adding an extra '/login' to url.

any idea what the problem is?

My main controller

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Login extends CI_Controller {
        public function index()
        {
            $this->load->view('login/header');
            $this->load->view('login/login_view');
            $this->load->view('login/footer');
        }
            public function validate()
            {
            $this->load->model('login_model');
            $this->load->library('form_validation');

            $this->form_validation->set_rules('username','Username', 'trim|required|min_length[6]|max_length[50]|valid_email|xss_clean');
            $this->form_validation->set_rules('password','Password', 'trim|required|xss_clean');

            //validation check
            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('login/header');
                $this->load->view('login/login_view');
                $this->load->view('login/footer');
            }
            else
            {
                $userdata['username'] = $_POST['username'];
                $userdata['password'] = md5($_POST['password']);

               //check whether user exist int the database or not 
                $result = $this->login_model->login_validation($userdata);

               //in case user exists                
               if($result == 1)
                {
                    $this->set_sessions_details();
                }
               //in case user doesnt exist
                else
                {
                    $data['flag'] = "Incorrect Username or Password!";
                    $this->load->view('login/header');
                    $this->load->view('login/login_view', $data);
                    $this->load->view('login/footer');
                }
            }
        }
//load a view in case user exists in the db.
        public function set_sessions_details()
        {
            $this->load->view('test');
        }
    }

My model to check user data in db.

login_model

 class Login_model extends CI_Model
 {
   function login_validation($userdata)
    {
      $user=$userdata['username'];
      $pass=$userdata['password'];
      $result = $this->db->get_where('users', array('username' => $user, 'password' => $pass));
      return $result->num_rows();
    }
 }
 ?>

My views:

login_view

<h3>Please Login</h3>
<form method='post' action='login/validate'>
  <div class="form-group" >
    <label for="username">Username</label>
    <input type="email" class="form-control" name="username" id="username" placeholder="Enter Username">
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" class="form-control" name="password" id="password" placeholder="Enter Password"> 
  </div>
  <button type="submit" class="btn btn-success">Login</button>

  <?php echo validation_errors('<p class"errors">');?>
</form>
<?php if (isset($flag)){ ?>
  <p><?php echo $flag; ?> </p>
<?php } ?>

header

<!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">
    <title>WFD</title>


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

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>

<div class='container'>
   <div class="header">
    <center><img src="<?php echo base_url();?>assets/images/logo.png" alt="logo" style="margin-top: 10px; padding-bottom: 20px; opacity: 1;">
      <h2>
         Welfare Foundation Database
      </h2>
    </center>
   </div>

Footer

    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="/ci/assets/js/bootstrap.min.js"></script>
      </body>
</html>

try using the form open tag in Form Helper link : https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

echo form_open('email/send');

which in the backend will create

See if this solves your issue.

I believe that, besides @Abhijit answer, your problem is the logic itself. You aren't redirecting to anywhere, and you should be. Besides you're calling repeated code unnecessarily, such as:

$this->load->view('login/header');
$this->load->view('login/login_view');
$this->load->view('login/footer');

This should only be called in one function,

public function index(){
   $this->load->view('login/header');
   $this->load->view('login/login_view');
   $this->load->view('login/footer');
}

public function validate(){
   // your validation logic

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

       $this->session->set_flashdata('error', array('msg' => 'Upps, something went wrong..', 'anotherParam' => 'anotherText'));

      // this will load again the views
      redirect('login');
      return;
   }
}

You are now able to maintain the form as you have:

<form method="POST" action="login/validate">
</form>

<?php if($this->session->flashdata('error')){ ?>
<p><?php echo $this->session->flashdata('error')->msg; ?></p>
<?php } ?>

Use url library in controllers:

$this->load->helper('url');

Use it on view

<form action="<?php echo base_url('login/validate'); ?>" method="POST">
    <!-- your input --> 
</form>