Link to home
Start Free TrialLog in
Avatar of Opeyemi AbdulRasheed
Opeyemi AbdulRasheed

asked on

Change Password Script

Hello there,

I don't know where the problem lies, for some reasons, the code bellow doesn't work.

Script:
<?php
require_once '../includes/connection.php';

function clean($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

if (! empty($_POST) ):

    $current_password = clean($_POST["current_password"]);
    $new_password = clean($_POST["new_password"]);
    $repeat_password = clean($_POST["repeat_password"]);
    $username = clean($_POST['username']);
    

    $userPass = $conn->prepare("SELECT Password FROM Users WHERE Username = ? LIMIT 1");
    $userPass->bind_param( "s", $username );
    $userPass->execute();
    $result = $userPass->get_result();
    $userPassword = $result->fetch_object();

    if ( $userPassword->Password == $current_password ):

        $query = "UPDATE Users SET Password = ? WHERE Username = ? ";
        $result = $conn->prepare($query);
        $result->bind_param( "ss", $new_password, $username );

        if ( $result->execute() ):

            $response = "<div class='alert alert-success'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button>
                Password changed successfully.</div>";
            die( $response );

        else:

            $response = "<div class='alert alert-danger'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button> ".$conn->error." </div>";

            die( $response );
        
        endif;
        
    endif;

endif;
?>

Open in new window


Ajax:
$(document).ready(function(){
        $('#changepass').click(function() {

                $.ajax({
                url     : 'change_pass.php',
                method  : 'post',
                data    : $('#changePassword-form').serialize(),
            }).done(function(response) {
                $('#response').html(response);

            });         
        });

    });

Open in new window


Modal form:
<!--Change Password Modal -->
<div id="changepass" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">User: <?=$session_username ?></h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <div id="response"></div>
                <div class="auth-content">
                    <p class="text-center">CHANGE PASSWORD</p>
                    <form id="changePassword-form" action="change_pass.php" method="POST" novalidate="">
                        <input type="hidden" name="username" id="username" value="<?=$session_username ?>"> 
                        <div class="form-group">
                            <label for="current_password">Old Password <span style=color:red;> *</span></label>
                            <input type="password" class="form-control underlined" name="current_password" id="current_password" placeholder="Your Old Password" required> 
                        </div>
                        <div class="form-group">
                            <label for="new_password">New Password <span style=color:red;> *</span></label>
                            <input type="password" class="form-control underlined" name="new_password" id="new_password" placeholder="Your New Password" required> 
                        </div>
                        <div class="form-group">
                            <label for="repeat_password">New Password <span style=color:red;> *</span></label>
                            <input type="password" class="form-control underlined" name="repeat_password" id="repeat_password" placeholder="Your New Password" required> 
                        </div>
                        <div class="form-group">
                            <button type="submit" name="changepass" class="btn btn-block btn-primary">Reset Password</button>
                        </div>
                        <div class="form-group">
                            <p class="text-muted text-center">Created by orpee2k2
                            </p>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div><!-- /.modal -->

Open in new window


Thanks for helping.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland 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 Opeyemi AbdulRasheed
Opeyemi AbdulRasheed

ASKER

Well spotted sir.

Eagle eyes you've got. Thanks