Link to home
Start Free TrialLog in
Avatar of Adebayo Ojo
Adebayo OjoFlag for Nigeria

asked on

Updating Password with Ajax

What am I doing wrongly in these code? I want to update user password without refreshing the page, but this is not working as my code stops at  
_("pmessage").innerHTML = 'please wait ...';

Open in new window

on the jquery/ajax code. The Ajax seems not to be firing as my DB is not updated with the new password. Below are my codes:

HTML
<form id='pform' method='post'>
<div id='entercurrent'>Enter Current Password:</div>
<input id='current' name='current' type='password'>
<div id='enternew'>Enter New Password:</div>
<input id='newpwd' name='newpwd' type='password'>
<div id='enterconfirm'>Confirm New Password:</div>
<input id='confirmpwd' name='confirmpwd' type='password'>
<input id='passsubmit' type='submit' value='Update Password'>
</form>
<div id='pmessage'></div>

Open in new window


Ajax
$(document).ready(function() {
    $("#passsubmit").click(function(event) {
        event.preventDefault();
        var cp = $("#current").val();
        var np = $("#newpwd").val();
        var cf = $("#confirmpwd").val();
        dataString = "cp=" + cp + "&np=" + np + "&cf=" + cf;
        _("passsubmit").style.display = "none";
        _("pmessage").innerHTML = 'please wait ...';

        $.ajax({
            type: "POST",
            url: "functions/changePwd.php",
            data: dataString,
            success: function() {}
        });
    });
});

Open in new window


PHP -  functions/changePwd.php
if(isset($_POST['cp'])) {
    $currentpwd = $_POST['cp'];
    $newpwd = password_hash($_POST['np'], PASSWORD_DEFAULT);
    $confirmnewpwd = password_hash($_POST['cf'], PASSWORD_DEFAULT);
}
$sql = "UPDATE users SET password='$newpwd' WHERE username='$log_username'";
$query = mysqli_query($db_connect, $sql);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Adebayo Ojo

ASKER

Thanks @gr8gonzo
I figured out the issue. It's actually due to include/require of script in a wrong sub-directory. It's working fine now.