Change password using javascript, php and mysqli.

<?php
session_start();
if(!$_SESSION['name'])
{?>
<script type="text/javascript">window.location.href='index.php';</script>
<?php
}
?>
<?php
if(isset($_POST["changepsw"]))
{
$name = $_SESSION['name'];
$oldpsw= $_POST["oldpsw"];
$newpsw= $_POST["newpsw"];
$conpsw= $_POST["conpsw"];
require("connection.php");
$qry1 = "SELECT * FROM user where name='$name';";
$re = mysqli_query($con,$qry1);
$row = mysqli_fetch_array($re);
$dbpsw= $row['password'];

if($dbpsw==$oldpsw)
{

if($newpsw==$conpsw)
{

$qrys="UPDATE user SET password='$newpsw' WHERE name='$name';";
$results= mysqli_query($con,$qrys);
if($results==true)
{
session_destroy();
?>
<script>alert("Password Change Successfully");
window.location.href = "index.php";
</script>
<?php
}
else
{
?>
<script>alert("Password Not Changed");
window.location.href = "change-psw.php";
</script>
<?php
}
mysqli_close($con);
}
else
{
?>
<script>alert("Password Not Matched");
window.location.href = "change-psw.php";
</script>
<?php
}
}
else
{
?>
<script>alert("Old password is wrong");
window.location.href = "change-psw.php";
</script>
<?php
}
}
?>

Welcome back to shortlearner.com , today we will see how to change password using JavaScript, PHP and MySQL.
first of all we should discuss about overall scenario which we used to reset password.
In first step we make a form (which have three input fields) with the help of HTML, CSS and Bootstrap.

change password in php
In the second step we match the new password and confirm password with the help of JavaScript.
in case if new password and confirm password are not matched than we will disable the submit button with the help of JQuery.
after hitting on the submit button first of all we run the select query and fetch the old password. than we match fetched password and inserted old password is matched or not. if condition is true than we move to next condition where we again check the new password and confirm password is matched or not if both are not matched than we show the error, if condition is matched than we will run update query and change the password of particular session id.

Also Read :
How to Install PHP on CentOS.
How to integrate Razorpay Payment Gateway using PHP.

PHP Login Script With Remember me.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

connection.php

<?php
$con= mysqli_connect("localhost","username","password","databasename");
 ?>

changepsw.php


index.html

<center><h2>Change Password</h2></center><hr>
<form method="post" action="#">
<label>Current Password</label>
		    <div class="form-group pass_show"> 
                <input type="password"  class="form-control" placeholder="Current Password" name="oldpsw"> 
            </div> 
		       <label>New Password</label>
            <div class="form-group pass_show"> 
              <input class="form-control " type="password" required=" " id="pass1" name="newpsw" placeholder="Password">
            </div> 
		       <label>Confirm Password</label>
            <div class="form-group pass_show"> 
               <input class="form-control " type="password" required=" " id="pass2" onkeyup="checkPass(); return false;" placeholder="Confirm Password" name="conpsw">
                                         <span id="confirmMessage"></span>
            </div> 
            <div class="form-group pass_show"> 
                <input type="submit"  class="btn btn-primary" value="Change Password" name="changepsw" id="cp"> 
            </div> 
            </form>

javascript

<script type="text/javascript">
  function checkPass()
{
    //Store the password field objects into variables ...
    var pass1 = document.getElementById('pass1');
    var pass2 = document.getElementById('pass2');
    //Store the Confimation Message Object ...
    var message = document.getElementById('confirmMessage');
    //Set the colors we will be using ...
    var goodColor = "#66cc66";
    var badColor = "#ff6666";
    //Compare the values in the password field 
    //and the confirmation field
    if(pass1.value == pass2.value){
        //The passwords match. 
        //Set the color to the good color and inform
        //the user that they have entered the correct password 
        pass2.style.backgroundColor = goodColor;
        message.style.color = goodColor;
        message.innerHTML = "Passwords Match!"
        $('#cp').prop('disabled', false);
    }else{
        //The passwords do not match.
        //Set the color to the bad color and
        //notify the user.
        pass2.style.backgroundColor = badColor;
        message.style.color = badColor;
        message.innerHTML = "Passwords Do Not Match!"
        $('#cp').prop('disabled', true);
    }
}  
</script>