General

Forgot Password in PHP Using AJAX and MySQL

Ravindra Kumar

User authentication is one of the most important parts of any web application. While creating a login and registration system in PHP is quite common, many developers forget to implement a secure Forgot Password feature.

A Forgot Password system allows users to reset their password in case they lose access. In this article, we’ll build a secure Forgot Password system using PHP, AJAX, and MySQL with email verification.

Workflow of Forgot Password in PHP

  • User submits email in the Forgot Password form.
  • System checks if the email exists in the users table.
  • A unique reset token is generated and stored in the database with expiry time.
  • An email with a reset link is sent to the user.
  • The user clicks the link and sets a new password.
  • The system verifies the token, updates the password, and clears the reset token.

Database Table Example (MySQL)


CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100),
email VARCHAR(100) UNIQUE,
password VARCHAR(255),
reset_token VARCHAR(255) NULL,
reset_expiry DATETIME NULL
);

This table stores user data and extra columns reset_token and reset_expiry for password reset.

Forgot Password Form (HTML + AJAX)


<form id="formForgotPassword">
<label>Email:</label>
<input type="email" name="email" required />
<button type="submit">Send Reset Link</button>
</form>

<div id="responseMessage"></div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$("#formForgotPassword").on("submit", function(e) {
e.preventDefault();
$.ajax({
url: "forgot_password_ajax.php",
type: "POST",
data: $(this).serialize(),
success: function(response) {
$("#responseMessage").html(response);
},
error: function() {
$("#responseMessage").html("AJAX Error Occurred.");
}
});
});
</script>

Backend: Send Reset Link (PHP + PHPMailer)


<?php
include 'db.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$email = $_POST['email'];

$stmt = $conn->prepare("SELECT * FROM users WHERE email=?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();

if($result->num_rows > 0) {
$token = bin2hex(random_bytes(32));
$expiry = date("Y-m-d H:i:s", strtotime('+1 hour'));

$update = $conn->prepare("UPDATE users SET reset_token=?, reset_expiry=? WHERE email=?");
$update->bind_param("sss", $token, $expiry, $email);
$update->execute();

$resetLink = "http://localhost/reset_password.php?token=".$token;

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "yourgmail@gmail.com";
$mail->Password = "your_app_password";
$mail->SMTPSecure = "tls";
$mail->Port = 587;

$mail->setFrom("yourgmail@gmail.com", "Your App");
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = "Reset Your Password";
$mail->Body = "Click this link to reset your password: <a href='$resetLink'>$resetLink</a>";
$mail->send();

echo "<span style='color:green;'>Reset link sent to your email.</span>";
} else {
echo "<span style='color:red;'>Email not found!</span>";
}
}
?>

Reset Password Page


<?php
include 'db.php';
if (isset($_GET['token'])) {
$token = $_GET['token'];

$stmt = $conn->prepare("SELECT * FROM users WHERE reset_token=? AND reset_expiry > NOW()");
$stmt->bind_param("s", $token);
$stmt->execute();
$result = $stmt->get_result();

if($result->num_rows > 0) {
echo '
<form id="resetForm">
<input type="hidden" name="token" value="'.$token.'">
<label>New Password:</label>
<input type="password" name="password" required />
<button type="submit">Reset Password</button>
</form>
<div id="resetMessage"></div>
';
} else {
echo "Invalid or expired link.";
}
}
?>

Reset Password via AJAX


<?php
include 'db.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$token = $_POST['token'];
$password = password_hash($_POST['password'], PASSWORD_BCRYPT);

$stmt = $conn->prepare("SELECT * FROM users WHERE reset_token=? AND reset_expiry > NOW()");
$stmt->bind_param("s", $token);
$stmt->execute();
$result = $stmt->get_result();

if($result->num_rows > 0) {
$update = $conn->prepare("UPDATE users SET password=?, reset_token=NULL, reset_expiry=NULL WHERE reset_token=?");
$update->bind_param("ss", $password, $token);
$update->execute();
echo "<span style='color:green;'>Password updated successfully! <a href='login.php'>Login</a></span>";
} else {
echo "<span style='color:red;'>Invalid or expired token.</span>";
}
}
?>

Final Thoughts

With this system, we’ve successfully built a Forgot Password feature in PHP with AJAX and MySQL.

  • Users can reset their password securely.
  • Emails are sent using SMTP (PHPMailer).
  • The token system ensures passwords can only be reset via a secure link.

This approach improves both security and user experience for your PHP application.

 
Download Forgot Password system with dashboard

Ravindra Kumar

Ravindra is a passionate full stack developer and dedicated blogger with a flair for crafting user-friendly web applications and insightful articles. With expertise spanning front-end and back-end technologies, Ravindra brings ideas to life through innovative coding solutions.

Suggested Reading

Download Free Pets & Animals Website Template

Create a Perfect Online Home for Your Pet Store or Animal Care Brand This Pets & Animals Website Template is designed for pet shops, grooming centers, veterinary clinics, animal shelters, and pet bloggers. With a cheerful layout, responsive design, and simple customization, it’s ideal for businesses that care for pets and animals with love. Template […]

eLearning Platform Website Template using HTML, CSS, Tailwind free Download

Looking to build a modern and responsive eLearning platform or online course website? Our eLearning Platform Template is designed using HTML, CSS, and Tailwind CSS, giving you a lightweight, fast, and customizable solution – and the best part, it’s absolutely free to download. Key Features Responsive Design – Built with Tailwind CSS for seamless viewing […]

(7)+ Platforms to Build AI Agents in 2025: A Complete Guide

Artificial Intelligence (AI) is no longer just about chatbots and smart recommendations. Today, AI agents are reshaping the way we work—automating tasks, reasoning through decisions, and collaborating like digital teammates. If you’re looking to create AI-powered agents for business or personal productivity, a growing number of platforms make it possible without heavy coding knowledge. In […]

The Most Reliable Hosting Options for Startups and SMBs

Small businesses are the backbone of any country. India accounts for over 63 million SMBs (Small and Medium Businesses) [ET Times]. Stepping up into this sector has its challenges, including a tight budget, growing demands, and the constant requirement to stay online. This is where a web hosting service becomes non-negotiable. Whether launching the first […]

Ecommerce Website Frontend source Code in HTML,CSS,Javascript & Bootstrap

Front-end source code makes up an eCommerce website’s UI. This code is crucial to buyer-site interaction.  It includes the structural, visual, and engaging parts that make the user’s experience unique. Online shopping Website frontend source code was created with HTML, CSS, JavaScript, and different frameworks, ensuring the website works and looks good. HTML: Structuring the Web Pages […]

How to assign a user ID to a guard in Laravel

1. Ensure Guard is Defined in auth.php In config/auth.php, make sure the college guard is properly configured: 'guards' => [ 'college' => [ 'driver' => 'session', 'provider' => 'colleges', // Must match the provider below ], ], 'providers' => [ 'colleges' => [ 'driver' => 'eloquent', 'model' => App\Models\College::class, // Update with your College model […]