dongliang9576 2015-01-18 10:16
浏览 74

与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>
  • 写回答

3条回答 默认 最新

  • dpjo15650 2015-01-28 14:54
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘