Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 467.5k

Resetting password to get opt on email

Apr 21 2023 1:19 PM

Hi Team

I am creating a logic that reset a password for users to get otp. But somehow this not working and need some help maybe i am experience something small. Let me share my logic below

// html code

<div class="container">
  <div class="row justify-content-center">
    <div class="col-md-6">
      <div class="card mt-5">
        <div class="card-header">
          Reset Password
        </div>
        <div class="card-body">
          <form id="reset-password">
            <div class="form-group">
              <label for="email">Email</label>
              <input type="email" class="form-control" id="email" name="email" required>
            </div>
            <div class="form-group">
              <label for="otp">OTP Code</label>
              <input type="text" class="form-control" id="otp" name="otp" required>
            </div>
            <div class="form-group">
              <label for="new-password">New Password</label>
              <input type="password" class="form-control" id="new-password" name="new-password" required>
            </div>
            <div class="form-group">
              <label for="confirm-password">Confirm Password</label>
              <input type="password" class="form-control" id="confirm-password" name="confirm-password" required>
            </div>
            <button type="submit" class="btn btn-primary">Reset Password</button>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

// php code

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

// Create a new PHPMailer instance
$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->isSMTP();
    $mail->Host       = '';
    $mail->SMTPAuth   = true;
    $mail->Username   = '';
    $mail->Password   = '';
    $mail->SMTPSecure = 'tls';
    $mail->Port       = 587;

    // Recipients
    $mail->setFrom('g', 'Gcobani');
    $mail->addAddress('', 'Your Name'); // Add a recipient

    // Content
    $mail->isHTML(true); // Set email format to HTML
    $mail->Subject = 'Test Email';
    $mail->Body    = 'This is a test email';

    // Send the email
    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

// jquery code

$(document).ready(function() {
  // Retrieve the email address from the URL
  
  // Display the email address in the form
  $('#email').val(email);

  // Submit the form with the OTP code
  $('#reset-password').submit(function(event) {
    event.preventDefault();
    var otp = $('#otp').val();
    $.ajax({
      url: 'reset-password.php',
      method: 'POST',
      data: { email: email, otp: otp },
      success: function(response) {
        if (response == 'success') {
          // Display a success message and redirect the user to the login page
          alert('Your password has been reset. Please log in with your new password.');
          
        } else {
          // Display an error message if the OTP code is incorrect
          alert('The OTP code you entered is incorrect. Please try again.');
        }
      }
    });
  });
});

 


Answers (5)