What's new

Help How to show password form

Status
Not open for further replies.

Alieson09

Forum Expert
Joined
Aug 30, 2021
Posts
2,246
Solutions
27
Reaction
2,962
Points
2,085

How to show password form

1682069818054.png

Code:
<?php
session_start();
include('includes/connect.php');

if (!isset($_SESSION['bpmsaid']) || strlen($_SESSION['bpmsaid']) == 0) {
 
} else {
  if(isset($_POST['submit'])) {
    $adminid=$_SESSION['bpmsaid'];
    $cpassword=($_POST['currentpassword']);
    $newpassword=($_POST['newpassword']);
    $query=mysqli_query($con,"select ID from login where ID='$adminid' and password='$cpassword'");
    $row=mysqli_fetch_array($query);
    if($row>0){
      $ret=mysqli_query($con,"update login set password='$newpassword' where ID='$adminid'");
      echo '<script>alert("Your password successully changed.")</script>';
    }
    else {
      echo '<script>alert("Your current password is wrong.")</script>';
    }
  }
}
?>
<head>
    <title>BPMS - Change Password</title>
<script type="text/javascript">
function checkpass()
{
if(document.changepassword.newpassword.value!=document.changepassword.confirmpassword.value)
{
alert('New Password and Confirm Password field does not match');
document.changepassword.confirmpassword.focus();
return false;
}
return true;
}
</script>
</head>
<body>
   <?php include_once('includes/header.php');?>
   <?php include_once('includes/navbar.php');?>

        <div class="container">
           
                <div class="row">
                    <div class="col-lg-6">
                        <div class="card">                        
                        </div> <!-- .card -->
                    </div><!--/.col-->

                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-header">
                                <strong>Change </strong> Password
                            </div>
                            <div class="card-body card-block">
                                <form action="" method="post" enctype="multipart/form-data" class="user" name="changepassword" onsubmit="return checkpass();">
                                     <?php
$adminid=$_SESSION['bpmsaid'];
$ret=mysqli_query($con,"select * from login where ID='$adminid'");
$cnt=1;
while ($row=mysqli_fetch_array($ret)) {
?>
                                <div class="form-group">
                                        <div class="col col-md-3"><label for="text-input" class=" form-control-label">Current Password</label></div>
                                        <div class="col-12 col-md-9"><input type="password" name="currentpassword" class=" form-control" required= "true" value=""></div>
                                    </div>
                                    <div class="row form-group">
                                        <div class="col col-md-3"><label for="email-input" class=" form-control-label">New Password</label></div>
                                        <div class="col-12 col-md-9"><input type="password" name="newpassword" class="form-control" value="" required="true"></div>
                                    </div>
                                    <div class="row form-group">
                                        <div class="col col-md-3"><label for="email-input" class=" form-control-label">Confirm Password</label></div>
                                        <div class="col-12 col-md-9"><input type="password" name="confirmpassword" class="form-control" value="" required="true"></div>
                                    </div>                                      
                                   
                                    <?php } ?>
                                   <p style="text-align: center;"> <button type="submit" class="btn btn-primary btn-user btn-block" name="submit" >Change</button></p>
                                </form>
                            </div>                          
                        </div>                      
                    </div>
                    <div class="col-lg-6">                
                </div>
            </div>
        </div><!-- .animated -->
    </div><!-- .content -->
</body>
</html>
    <?php
include('includes/scripts.php');
include('includes/footer.php');
?>
 

Attachments

This code shows a form to change the password of an admin in a website. The form requests the current password, the new password and confirmation of the new password. The form also has a checkpass() function that checks if the new password and the confirmation of the new password field match.

The PHP code first checks if the admin is logged in or not. If the admin is logged in, it checks if the form has been submitted. If so, it gets the admin ID and the values of the current password, the new password, and confirmation of the new password fields. It then checks if the entered current password matches the password stored in the database. If it matches, it updates the password in the database with the new password. If it does not match, it displays an error message.

The form uses Bootstrap classes for styling. It also includes the header, navbar, and footer files.
 
Status
Not open for further replies.

Similar threads

Back
Top