0

不知何故,我的 codeigniter 表单验证未显示在登录页面上,下面给出的是我的登录页面

            <div class="full_w">
        <p>Food4U</p>
        <?php echo validation_errors(); ?>
            <?php echo form_open('staff/valid_login'); ?>
                <label for="city">Select City</label>
                <select name="city" class="text">
                    <option value="">Select City</option>
                    <?php echo $this->front_model->get_Fcities(); ?>
                </select>
                <label for="login">Username:</label>
                <input id="login" name="username" class="text" />
                <label for="pass">Password:</label>
                <input id="pass" name="password" type="password" class="text" />
                <div class="sep"></div>
                <button type="submit" class="ok">Login</button>
                <a class="button" href="">Change Password</a>
            <?php echo form_close(); ?>
            </form>
        </div>

下面给出的是控制器中的功能

    function valid_login()
{
    $this->form_validation->set_rules('city','City','required|trim|xss_clean|callback_validate_credentials');
    $this->form_validation->set_rules('username','Username','required|trim|xss_clean|callback_validate_credentials');
    $this->form_validation->set_rules('password','Password','required|trim');
    if($this->form_validation->run())
    {
        $data = array(
                'username' =>$this->input->post('username'),
                'is_loggedin' => 1
                );
            $this->session->set_userdata($data);
            redirect('staff/member');
    }
    else 
    {
        $this->load->view("dashboard/login");
    }

 }

 function member()
 {   
    $row['content']=$this->load->view("dashboard/member",array(),true);
    $this->load->view("dashboard/template", $row);
 }

function validate_credentials() 
{
    if($this->staff_model->can_login())
    {
        return true;
    }
    else
    {
        $this->form_validation->set_message('validate_credentials','Incorrect Username or Password');
        return false;
    }
}

我的模型函数是

    function can_login()
{
    $this->db->where('DB_User_City',$this->input->post('city'));
    $this->db->where('DB_User_Login',$this->input->post('username'));
    $this->db->where('DB_User_Pass',$this->input->post('password'));

    $query= $this->db->get('frp_db_user');

    if($query->num_rows() == 1)
    {
        return true;
    }
    else 
    {
        return false;
    }
}

如果用户只是单击提交按钮,用户应该会看到一条消息,并且如果用户提供的任何单个值不正确,那么用户还必须查看登录有什么问题,以便用户可以提供正确的信息

4

2 回答 2

0

您的代码似乎还可以。试试这个

if($this->form_validation->run() == FALSE)
{ 
    //redirect to your login form
}
else
{
    //redirect to dashboard
}

如果您遇到任何问题,请告诉我。

于 2013-07-22T07:14:04.677 回答
0

在您设置验证的每个字段的视图中,将其字段或其他地方写成这样的代码来显示您的验证错误。

      <div class="full_w">
    <p>Food4U</p>
    <?php echo validation_errors(); ?>
        <?php echo form_open('staff/valid_login'); ?>
            <label for="city">Select City</label>
            <select name="city" class="text">
                <option value="">Select City</option>
                <?php echo $this->front_model->get_Fcities(); ?>
            </select>
            <br />
            <?=form_error('city')?>
            <br />
            <label for="login">Username:</label>
            <input id="login" name="username" class="text" />
            <br />
            <?=form_error('username')?>
            <br />
            <label for="pass">Password:</label>
            <input id="pass" name="password" type="password" class="text" />

            <br />
            <?=form_error('password')?>
            <br />

            <div class="sep"></div>
            <button type="submit" class="ok">Login</button>
            <a class="button" href="">Change Password</a>
        <?php echo form_close(); ?>
        </form>
    </div>
于 2013-07-22T07:44:17.423 回答