'How do I fix the error on my password confirmation field

I'm having an issue with my PHP code where my password confirmation field keeps asking me to confirm my password even thought I did add it and matches.

Here is my code:

            if(empty($data['password'])){
                $data['pass_err'] = 'Pleae enter password';
              } elseif(strlen($data['password']) < 6){
                $data['pass_err'] = 'Password must be at least 6 characters.';
              }

            // Validate confirm password 
            if(empty($data['confirm_password'])){
                $data['confirm_pass_err'] = 'Pleae confirm password';
            } else {
                    if ($data['password'] !== $data['confirm_password']){
                        $data['confirm_pass_err'] = 'Passwords do not match.';
                    }
                }

My form:

<form action="<?php echo LINKROOT; ?>/users/register" method="post">
                <div class="form-group">
                    <input type="text" name="name" class="form-control form-control-md <?php echo (!empty($data['name_err'])) ? 'is-invalid' : '' ?>" value="<?php echo $data['name']; ?>" placeholder="Your Name">
                    <span class="invalid-feedback"><?php echo $data['name_err']; ?></span>
                </div>
                <br>
                <div class="form-group">
                    <input type="email" name="email" class="form-control form-control-md <?php echo (!empty($data['email_err'])) ? 'is-invalid' : '' ?>" value="<?php echo $data['email']; ?>" placeholder="Your Email Address">
                    <span class="invalid-feedback"><?php echo $data['email_err']; ?></span>
                </div>
                <br>
                <div class="form-group">
                    <input type="password" name="password" class="form-control form-control-md <?php echo (!empty($data['pass_err'])) ? 'is-invalid' : '' ?>" value="<?php echo $data['password']; ?>" placeholder="Your Password">
                    <span class="invalid-feedback"><?php echo $data['pass_err']; ?></span>
                </div>
                <br>
                <div class="form-group">
                    <input type="password" name="confirm_password" class="form-control form-control-md <?php echo (!empty($data['confirm_pass_err'])) ? 'is-invalid' : '' ?>" value="<?php echo $data['confirm_password']; ?>" placeholder="Confirm Your Password">
                    <span class="invalid-feedback"><?php echo $data['confirm_pass_err']; ?></span>
                </div>
                <br>
                <div class="form-group">
                    <div class="row text-center">
                        <div class="col">
                            <input type="submit" style="width:100%" value="Sign Up" class="btn btn-success btn-block">
                        </div>
                        <div class="col">
                            <a href="<?php echo LINKROOT; ?>/users/login" style="width:100%" class="link-secondary">Have an account? Sign In</a>
                        </div>
                    </div>
                </div>
                </div>
            </form>

I've also attached a screenshot for reference

Screenshot of password confirm error



Solution 1:[1]

  1. ensure that $data['confirm_password'] is not emptified by the code after posting.
  2. try code below.
    // Validate password and confirm_password
    if (isset($data['password']) && isset($data['confirm_password']) && is_string($data['password']) && is_string($data['confirm_password'])) {
        $len_p = strlen($data['password']);
        $len_cp = strlen($data['confirm_password']);
        // valid case
        if ($len_p >= 6 && $data['password'] === $data['confirm_password']) {
            // save user
        }
        // invalid case
        else {
            if (0 === $len_p) {
                // password is empty
                // ..
                return;
            }
            if (6 > $len_p) {
                // password is short
                // ..
                return;
            }
            if ($data['password'] !== $data['confirm_password']) {
                // passwords don't match
                // ..
                return;
            }
        }
    }
    else {
        // $data is improper
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ersin