密码更改时重定向到主页

时间:2014-06-20 14:00:09

标签: php codeigniter netbeans url-redirection ajaxform

我正在使用一些php,如果密码已成功更改,我需要重定向页面。目前,当我点击提交时它会更改密码,但没有任何反应。我有点坚持这个,这是我第一次与PHP合作,所以请各位帮忙。下面是我的型号代码。

public function change_password($user_id, $hash)
{
    $query = "UPDATE users SET password=? WHERE user_id=?";

    if($this->db->query($query, array($hash, $user_id))){

        return TRUE;

    }
    else{
        return FALSE;
    }
}

控制器:

     public function security_settings(){
    $pass = $this->input->post('password', TRUE);
    $cpass = $this->input->post('cpassword', TRUE);
    $user_id = $this->user->user_id;

    if($pass==='' || $cpass==='')
    {
        echo '{"msg":"alert alert-danger","html":"Passwords do not match"}';
        return;
    }

    if($pass!=$cpass)
    {
        echo '{"msg":"alert alert-danger","html":"Passwords do not match"}';
        return;
    }

    $hash = $this->phpass->hash($pass);
    while(strlen($hash) < 20){
        $hash = $this->phpass->hash($pass);
    }

    if($this->profile_model->change_password($user_id, $hash))
    {
        echo '{"msg":"alert alert-info","html":"Changes have been saved successfully"}';
    }
    else
    {
        echo '{"msg":"alert alert-danger","html":"<b>Error!</b> Unable to save changes"}';
    }
}

这是观点:

    <form  class="form-horizontal" role="form" id="defform" method="post" action="<?php echo site_url('profile/security_settings') ?>">

            <div class="form-group">
                <label class="control-label col-md-3" for="password">New Password</label>
                <div class="col-md-5">
                    <input type="password" class="form-control" id="password" name="password" placeholder="Enter new password">
                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-md-3" for="cpassword">Confirm Password</label>
                <div class="col-md-5">
                    <input type="password" class="form-control" id="cpassword" name="cpassword" placeholder="Confirm new password">
                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-md-3"></label>
                <div class="col-md-5">
                    <button type="submit" class="btn btn-primary">Save Changes

                    </button>

                </div>
            </div>
        </form> 

这是我用于ajax表单提交的javascript代码:

    <script type="text/javascript">
$().ready(function() {

    $('#defform').bootstrapValidator({
        message: 'This value is not valid',
        fields: {
            password: {
                validators: {
                    notEmpty: {
                        message: 'The password is required and can\'t be empty'
                    },
                    stringLength: {
                        min: 8,
                        max: 50,
                        message: 'The password must be more between 8 to 50 characters long'
                    },
                    identical: {
                        field: 'password2',
                        message: 'The password and its confirm are not the same'
                    }
                }
            },
            cpassword: {
                validators: {
                    notEmpty: {
                        message: 'The confirm password is required and can\'t be empty'
                    },
                    stringLength: {
                        min: 8,
                        max: 50,
                        message: 'The password must be more between 8 to 50 characters long'
                    },
                    identical: {
                        field: 'password',
                        message: 'The password and its confirm are not the same'
                    }
                }
            }
        }
    });


    $('#defform').ajaxForm({
        dataType: 'json',
        success: function(response){
                    $('#info-well').removeClass().addClass(response.msg);
                    $('#info-well').html(response.html);
                 }
    });


});

1 个答案:

答案 0 :(得分:0)

类似于msghtmlredirectedpage添加2个值 一个例子可以像

'{"msg":".....","html":".....","redirect":1,"page":base_url()."/controller"} // if you want a redirect.

'{"msg":".....","html":".....","redirect":0,"page":"#"} // if you do not want a redirect.

你将拥有

    $('#defform').ajaxForm({
            dataType: 'json',
            success: function(response){
                        $('#info-well').removeClass().addClass(response.msg);
                        $('#info-well').html(response.html);
                         //you can set a timeout here of say 5 seconds so that user can read the success message and append text like redirecting to.....
                         if(response.redirected==1) {window.location.href = response.page;} //this line will redirect to the controller.

                     }
        });