0

Trying to get page to dynamically show if passwords match (echo 'Match' or 'Do not match'). (Similiar to previous question asked here.)

Looking to check if password1 and password2 are the same as you type in either the 'password' or 'confirm password' input boxes.

EDIT: Seems elseif ($password2 == $password1) isn't checking for the match as I intended. Posting of password1 and password2 works (#feedback changes to 'Do not match' as you type in either input box, but 'Match' never shows). Any help on how to fix this code is appreciated.

check.php

<?php
include __DIR__ . '/mysqli_connect.php';

$password2 = mysqli_real_escape_string($dbc, $_POST['password2']);
$password1 = mysqli_real_escape_string($dbc, $_POST['password1']);

if ($password2==NULL && $password1==NULL) {
    echo '';
} elseif ($password2 == $password1) {
    echo 'Match';
} else {
    echo 'Do not match';
}

register.php

<script type="text/javascript" src="/jquery-3.2.1.min.js"></script>

<script>
$(document).ready(function() {
    $('#feedback').load('/includes/check.php').show();

    $('#password1').keyup(function() {
        $.post('/includes/check.php', { password1: form.password1.value }, 
        function(result) {
            $('#feedback').html(result).show;
        });
    });
});

$(document).ready(function() {
    $('#feedback').load('/includes/check.php').show();

    $('#password2').keyup(function() {
        $.post('/includes/check.php', { password2: form.password2.value }, 
        function(result) {
            $('#feedback').html(result).show;
        });
    });
});
</script>

<form action="register.php" method="post" name="form">

<input id="password1" class="signup_input_box" type="password" name="password1" maxlength="20" value="<?php if (isset($trimmed['password1'])) echo $trimmed['password1']; ?>">

<input id="password2" class="signup_input_box" type="password" name="password2" maxlength="20" value="<?php if (isset($trimmed['password2'])) echo $trimmed['password2']; ?>">
<div id="feedback"></div>

<p><center><input type="submit" name="submit" value="Register"></center>
</form>
Qwerty
  • 75
  • 8
  • And your problem/question is? – j08691 Sep 30 '17 at 19:59
  • Sorry, the code isn't working. Looking for any help as to what I did wrong. – Qwerty Sep 30 '17 at 19:59
  • Which code isn't working? – j08691 Sep 30 '17 at 20:00
  • can you describe your problem with your code? Or can you write down your error code? – episch Sep 30 '17 at 20:03
  • the 'elseif ($password2 == $password1)'. When I type the same pw into both input boxes, 'Match' doesn't show, only the 'Do not match' echo does. – Qwerty Sep 30 '17 at 20:04
  • to compare your pw dont use this function before: `$password2 = mysqli_real_escape_string($dbc, $_POST['password2']); $password1 = mysqli_real_escape_string($dbc, $_POST['password1']);` that make a resource from your string. and this didnt compareble ! – episch Sep 30 '17 at 20:09
  • I don't understand what you mean. What should I do? – Qwerty Sep 30 '17 at 20:17

1 Answers1

0

Skipped using MySql posting and used JS only to check the value of the password inputs and show text upon keyup. Reference here.

Qwerty
  • 75
  • 8