且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

简单的Codeigniter表单验证

更新时间:2023-12-03 18:37:52

您知道验证中的问题,您正在检查类索引方法中的验证,而不是您的类和表单的登录方法已对ci_login / login方法采取了措施,如果登录失败,它将进行重定向,并且其清除表单验证也会验证您在登录方法上的字段,并将所有错误消息放入会话和显示中,脚本的某些更改已在此链接上查看代码已修改

you know problem in your validation you're checking validation in index method of your class not in login method of your class and in your form you have given action to ci_login/login method which is redirecting if login is failed and its clearing form validation validate your fields on login method also and put all error message in session and display, i have made some changes in script have look here on this link Code modified

为此更改,您必须使用url帮助器

for this changes you have to use url helper

function index()
{
    $this->form_validation->set_rules('username','Username','trim|required|exact_length[4]|xss_clean');
    $this->form_validation->set_rules('password','Password','trim|required|min_length[4]|max_length[40]|xss_clean|callback_login');
    $this->form_validation->set_rules('jabatan','Jabatan','trim|required|xss_clean');

    if($this->form_validation->run() == false)
    {
        $this->load->view('login');
    }
    else
    {
        //to check if the validation run correctly
        //$this->load->view('welcome_message');

        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $jabatan = $this->input->post('jabatan');

        $value = $this->m_login->login($username,$password,$jabatan);

        if($value)
        {
            redirect('welcome_message');
            //return true;
        }
        else
        {
            $this->form_validation->set_message('login', 'password salah');
            redirect('c_login',$login); //i want to pass $login into login form, then print
            return false;               //them as a form_error

        }
    }

}


<?php echo form_open(uri_string()); ?>
<table>
    <tr>
        <td>Username</td>
        <td><?php $inusername=array('name' => 'username', 'class' => 'GUI'); echo form_input($inusername); ?></td>
        <td class="error"><?php echo form_error('username'); ?></td>
    </tr>

    <tr>
        <td>Password</td>
        <td><?php $inpassword=array('name' => 'password', 'class' => 'GUI', 'type' =>'password'); echo form_input($inpassword); ?></td>
        <td class="error"><?php echo form_error('password'); echo $this->session->flashdata('login'); ?></td>
    </tr>

    <tr>
        <td>Jabatan</td>
        <td><?php $injabatan=array('keuangan' => 'keuangan', 'admin' => 'admin', 'hd' => 'head divisi', 'direktur' => 'direktur'); echo form_dropdown('jabatan',$injabatan,'keuangan','class = "gui"'); ?></td>
        <td class="error"><?php echo form_error('jabatan'); ?></td>
    </tr>

     <tr>
        <td></td>
        <td><?php $insubmit=array('name' =>'login','class' =>'button','value' => 'Login'); echo form_submit($insubmit); echo nbs(); $inreset=array('name' =>'reset','class' =>'button','value' => 'Hapus'); echo form_reset($inreset); ?></td>
        <td class="error"><?php echo form_error(); ?></td>
    </tr>
</table>
<?php echo form_close(); ?>