• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
password matches not working

#1
I'm stuck in this part. Everytime I correctly inputted the two fields with the same value it always returning password does not match.
I'm using ajax btw
Here's my controller:
Code:
$this->form_validation->set_rules("password", "password", "required");
        $this->form_validation->set_rules("passwordconf", "password confirm", "matches[password]");
        $data = array('userPassword' => password_hash($this->input->post('password'), PASSWORD_BCRYPT));
     
        if ($this->form_validation->run()) {
            $id = $this->input->post('id');
            $response['success'] = true;
            $response['message'] = "Successfully updated user password";
            $response['page'] = base_url();
            $this->user_model->userupdate($id, $data);
            $response['id'] = $id;
            $response['data'] = $data;
            
        } else {
            $response['success'] = false;
            $response['errors'] = $this->form_validation->error_array();
        }

Here's my ajax code: 
Code:
$('#changePassForm').on('submit',function(event){
    var userID = $('#userID').val();
    var password = $('#password').val();
    event.preventDefault();
    $.ajax({
        url: site_url('user/userUpdate'),
        data: {id: userID, password: password},
        type: 'post',
        dataType: 'json',
        success: function(response){
            if(response.success === true){
                console.log(response);
                Materialize.toast(response.message, 2000, 'green');
                setTimeout(function(){window.location.href = response.page;},3000);
            }else{
                if(response.success === false) {
                    if (response.errors) {
                        $.each(response.errors, function (key, val) {
                            $('input[name="' + key + '"]').next().html(val).addClass('has-error');
                        });
                    }
                }
            }
        }
    });
});

My view: 
Code:
<div class="row" id="login-box" style="padding-top:140px">
    <div class="col s4 offset-s4">
        <div class="z-depth-5 card-panel blue-grey darken-4 white-text">
            <h4 class="center">Change your password</h4>
            <form autocomplete="off" id="changePassForm">
                <div class="row">
                    <input type="hidden" id="userID" value="<?php echo $this->session->userdata('userID');?>">
                    <div class="input-field col s12">
                        <label for="last_name">Username</label>
                        <input id="username" name="username" type="text" style="font-size: 1.3rem;" class="" readonly value="<?php echo $this->session->userdata('userName');?>">
                        <span class="red-text"></span>
                    </div>
                    <div class="row">
                        <div class="input-field col s12">
                            <label for="password">New Password</label>
                            <input id="password" name="password" type="password" class="validate">
                            <span class="red-text"></span>
                        </div>
                        <div class="input-field col s12">
                            <label for="passwordconf">Confirm Password</label>
                            <input name="passwordconf" type="password" class="validate">
                            <span class="red-text"></span>
                        </div>
                    </div>
                    <div class="row" style="margin-bottom: 0px;">
                        <button type="submit" id="btn_changepass" class="btn red darken-4 waves-effect waves-light col s12"> Change Password</button>
                    </div>
            </form>
        </div>
    </div>
</div>

I seem no error IMO. Where do I get wrong here.
Reply

#2
(08-01-2017, 09:55 AM)nullstrike Wrote: I'm stuck in this part. Everytime I correctly inputted the two fields with the same value it always returning password does not match.
I'm using ajax btw
Here's my controller:
Code:
$this->form_validation->set_rules("password", "password", "required");
        $this->form_validation->set_rules("passwordconf", "password confirm", "matches[password]");
        $data = array('userPassword' => password_hash($this->input->post('password'), PASSWORD_BCRYPT));
     
        if ($this->form_validation->run()) {
            $id = $this->input->post('id');
            $response['success'] = true;
            $response['message'] = "Successfully updated user password";
            $response['page'] = base_url();
            $this->user_model->userupdate($id, $data);
            $response['id'] = $id;
            $response['data'] = $data;
            
        } else {
            $response['success'] = false;
            $response['errors'] = $this->form_validation->error_array();
        }

Here's my ajax code: 
Code:
$('#changePassForm').on('submit',function(event){
    var userID = $('#userID').val();
    var password = $('#password').val();
    event.preventDefault();
    $.ajax({
        url: site_url('user/userUpdate'),
        data: {id: userID, password: password},
        type: 'post',
        dataType: 'json',
        success: function(response){
            if(response.success === true){
                console.log(response);
                Materialize.toast(response.message, 2000, 'green');
                setTimeout(function(){window.location.href = response.page;},3000);
            }else{
                if(response.success === false) {
                    if (response.errors) {
                        $.each(response.errors, function (key, val) {
                            $('input[name="' + key + '"]').next().html(val).addClass('has-error');
                        });
                    }
                }
            }
        }
    });
});

My view: 
Code:
<div class="row" id="login-box" style="padding-top:140px">
    <div class="col s4 offset-s4">
        <div class="z-depth-5 card-panel blue-grey darken-4 white-text">
            <h4 class="center">Change your password</h4>
            <form autocomplete="off" id="changePassForm">
                <div class="row">
                    <input type="hidden" id="userID" value="<?php echo $this->session->userdata('userID');?>">
                    <div class="input-field col s12">
                        <label for="last_name">Username</label>
                        <input id="username" name="username" type="text" style="font-size: 1.3rem;" class="" readonly value="<?php echo $this->session->userdata('userName');?>">
                        <span class="red-text"></span>
                    </div>
                    <div class="row">
                        <div class="input-field col s12">
                            <label for="password">New Password</label>
                            <input id="password" name="password" type="password" class="validate">
                            <span class="red-text"></span>
                        </div>
                        <div class="input-field col s12">
                            <label for="passwordconf">Confirm Password</label>
                            <input name="passwordconf" type="password" class="validate">
                            <span class="red-text"></span>
                        </div>
                    </div>
                    <div class="row" style="margin-bottom: 0px;">
                        <button type="submit" id="btn_changepass" class="btn red darken-4 waves-effect waves-light col s12"> Change Password</button>
                    </div>
            </form>
        </div>
    </div>
</div>

I seem no error IMO. Where do I get wrong here.


in u'r controller, u seems like trying to validate 'password' and 'passwordconf'
PHP Code:
$this->form_validation->set_rules("password""password""required");
$this->form_validation->set_rules("passwordconf""password confirm""matches[password]"); 

but the POST VARS that you're trying to send to server using AJAX is only 'password'
Code:
data: {id: userID, password: password}

so... that kind of error is to be expected.
because your controllers tried to validate 'passwordconf' which is nonexistant
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.