如何在codeigniter中更改密码并使用jquery验证字段

时间:2014-05-24 13:50:13

标签: php codeigniter

我的观看页面为password.php

<form>
    oldpassword<input type="password" name="oldpassword" id="oldpassword"/><br>

    newpassword<input type="password" name="newpassword" id="newpassword"/><br>

    confirmpassword<input type="password" name="confirmpassword" id="confirmpassword"/><br>

    <input type="submit" name="submit" value="changepassword"/>
</form>

控制器page.password中应放置哪些代码必须为MD5

1 个答案:

答案 0 :(得分:0)

我的脚本中没有使用jQuery,如果你愿意,你可以通过jQuery更改提交功能....我没有使用jQuery

public function saveData(){
    $this->form_validation->set_rules ( "oldpassword", "Old Password", "trim|required|xss_clean|callback_passwordCheck" );
    $this->form_validation->set_rules ( "newpassword", "New Password", "trim|required|matches[repassword]|xss_clean|min_length[5]" );
    $this->form_validation->set_rules ( "repassword", "Confirm Password", "trim|required|xss_clean" );
    if ($this->form_validation->run () == false) {
        $this->updatePassword ();
    }else{
        $md5pass = $this->input->post('newpassword');
        $data ['username'] = $this->session->userdata ( 'username' );
        $data ['password'] = md5($md5pass);

        $this->load->model('loginModel');
        $this->loginModel->updatePassword('username',$data ['username'],$data);

        //var_dump($data); 
    }
}

public function passwordCheck(){
    $username = $this->session->userdata ( 'username' );
    $md5pass = $this->input->post('oldpassword');
    $this->load->model('loginModel');
    $results = $this->loginModel->getOldPassword($username);
    $currentPass = $results->password;

    if($md5pass==$currentPass){
        return true;
    }else{
        $this->form_validation->set_message('passwordCheck', 'Invalid current password, please try again');
        return false;
    }
}

<?php echo form_open("loginController/savedata",'class="cmxform form-horizontal tasi-form" id="commentForm"')?>

            <div class="form-group">
              <label class="col-sm-2 control-label">Old Password</label>
              <div class="col-sm-10">
                <?=form_input($fldoldPassword) ?>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 control-label">New Password</label>
              <div class="col-sm-10">
                <?=form_input($fldNewPassword) ?>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 control-label">Confirm Password</label>
              <div class="col-sm-10">
                <?=form_input($fldRePassword) ?>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 control-label">&nbsp;</label>
              <div class="col-sm-10">
                <button class="btn btn-danger" type="submit">Save</button>
                <button class="btn btn-default" type="button">Cancel</button>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 control-label">&nbsp;</label>
              <div class="col-sm-10">
                <?=validation_errors('<p class="alert alert-block alert-danger fade in">'); ?>
              </div>
            </div>
          </form>