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
Reset Password
// php code
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.');
}
}
});
});
});
Tuhin PaulPosted Apr 22, 2023, 1:34 AM
Part 4
Explanation of all the HTML, PHP, jQuery codes:
1. The PHP code uses the PHPMailer library to send an email. You need to configure the SMTP settings, including the SMTP server, authentication credentials, and SMTP port.
2. The jQuery code retrieves the email address from the URL using the URLSearchParams API and sets the value of the email input field.
3. When the user submits the form, the jQuery code sends an AJAX request to the reset-password.php file with the email and OTP code as data.
4. If the OTP is correct, the PHP file sends an email to the user. In this case, the PHP file is only sending a test email, so you will need to modify the code to send a password reset email with a link to reset the password.
5. If the AJAX request is successful, the jQuery code displays a success message and redirects the user to the login page. If the OTP code is incorrect, it displays an error message.
Tuhin PaulPosted Apr 22, 2023, 1:34 AM
Part 3
jQuery Code:
Tuhin PaulPosted Apr 22, 2023, 1:32 AM
Part 2
Here is the HTML code:
PHP Code:
Tuhin PaulPosted Apr 22, 2023, 1:30 AM
Part 1
1. In the HTML code, the variable "email" is not defined. You need to retrieve the email address from the URL or from a database, and then assign it to the "email" variable.
2. In the PHP code, you need to check the OTP code and send an email to the user with a link to reset their password. The current code only sends a test email and does not validate the OTP code or generate a password reset link.
3. In the jQuery code, you need to handle the response from the server correctly. The current code only displays an alert message, but does not redirect the user to the login page or display any error messages.
Rajkiran SwainPosted Apr 21, 2023, 1:53 PM
It appears that you have not implemented the password reset logic in the code you shared. Currently, the PHP code only sends a test email and does not include any password reset logic.
To implement a password reset logic, you need to:
Here's a general outline of the password reset logic you can follow:
// PHP code for sending OTP code to the user's email address $email = $_POST['email'];
// Generate a random OTP code and store it in a session variable or the database $otp = rand(1000, 9999); $_SESSION['reset_otp'] = $otp;
// Send the OTP code to the user's email address using PHPMailer or any other email sending library $mail = new PHPMailer(true);
// Set up the email message $mail->addAddress($email); $mail->Subject = 'Password Reset OTP'; $mail->Body = 'Your OTP code is ' . $otp;
// Send the email $mail->send();
// PHP code for verifying the OTP code and resetting the password $email = $_POST['email']; $otp = $_POST['otp']; $new_password = $_POST['new-password'];
// Check if the OTP code entered by the user matches the one generated earlier if ($otp == $_SESSION['reset_otp']) { // Update the user's password with the new password entered by the user // You will need to write code to update the password in your database // Once the password is updated, you can display a success message to the user // and redirect them to the login page } else { // Display an error message if the OTP code is incorrect // You can use the following code to display an error message using jQuery alert('The OTP code you entered is incorrect. Please try again.'); }
Note that this is just a general outline, and you will need to write the actual code for updating the password in your database and displaying the appropriate success/error messages to the user.