Greetings of the day!!!
While I was working on a web project with PHP and MySQL, there was a requirement to create a LOGIN form in a popup box instead of creating a login page separately. This provides a better user experience by not redirecting to another login page. This write-up will help you create a login form in a popup box using jQuery.
The pop up login box looks as below:
You can download the close button image used in the examples here: https://sites.google.com/site/karjolblogs/downloads/close.png?attredirects=0&d=1
To create this popup login, follow these four steps:
- Make an HTML file and define markups for the Login Form and Popup Box
- Make a CSS file and define styling for the Login Form and Popup Box
- Make a script file and define effects for the Login Form and Popup Box (jQuery)
- Make a PHP file and perform server-side login
1) HTML (login.html)
Create a simple page with a button to trigger the popup. Save as login.html. This example references loginstyle.css, jQuery and logineffect.js.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" type="text/css" href="login_style.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <!-- or your local jquery.js -->
<script src="login_effect.js"></script>
</head>
<body>
<div class="container">
<button id="show_login" type="button">Show Login</button>
<div id="loginform" role="dialog" aria-modal="true" aria-hidden="true">
<form method="post" action="dologin.php">
<p>You need to login to clear the data</p>
<!-- close button (add alt text for accessibility) -->
<img id="close_login" src="images/close.png" alt="Close login dialog" />
<input type="text" id="login" name="uid" placeholder="Email Id" />
<input type="password" id="password" name="upass" placeholder="***" />
<input type="submit" id="dologin" value="Login" />
</form>
</div>
</div>
</body>
</html>
Notes:
- The example uses a separate dologin.php script for server-side login handling.
- I included a CDN link to jQuery; you can use a local jquery.js instead.
2) CSS (login_style.css)
Below is the styling used for the popup. I converted fixed absolute offsets into a centered, responsive modal using transform so it works across viewport sizes. Save as login_style.css.
body {
background-color: #BDBDBD;
font-family: helvetica, Arial, sans-serif;
}
/* Modal container (hidden by default) */
#loginform {
width: 500px;
max-width: 90%;
height: auto;
min-height: 200px;
padding: 10px;
background-color: #585858;
border-radius: 3px;
box-shadow: 0 0 10px #424242;
box-sizing: border-box;
color: #E6E6E6;
/* center on screen */
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
visibility: hidden;
display: none;
z-index: 1000;
}
#loginform #close_login {
position: absolute;
top: 10px;
right: 10px;
width: 20px;
height: 20px;
cursor: pointer;
}
#loginform p {
margin-top: 40px;
font-size: 22px;
color: #E6E6E6;
}
#loginform #login,
#loginform #password,
#loginform #dologin {
display: block;
width: 250px;
height: 40px;
margin: 5px 0;
padding: 5px;
border: 2px solid silver;
border-radius: 3px;
box-sizing: border-box;
font-size: 16px;
}
#loginform #dologin {
background: grey;
color: #E6E6E6;
border: none;
cursor: pointer;
}
/* optional overlay (if you want a dim background) */
.modal-overlay {
position: fixed;
top: 0; left: 0; right: 0; bottom: 0;
background: rgba(0,0,0,0.5);
display: none;
z-index: 999;
}
3) JavaScript (login_effect.js)
A simple jQuery script to show/hide the popup. Save as login_effect.js.
$(document).ready(function(){
$("#show_login").click(function(){
showpopup();
});
$("#close_login").click(function(){
hidepopup();
});
// close on Esc
$(document).on('keydown', function(e) {
if (e.key === 'Escape') {
hidepopup();
}
});
});
function showpopup() {
$("#loginform").fadeIn();
$("#loginform").css({"visibility":"visible","display":"block","aria-hidden":"false"});
$("#loginform").attr('aria-hidden', 'false');
// focus the first input for accessibility
setTimeout(function(){ $('#login').focus(); }, 200);
}
function hidepopup() {
$("#loginform").fadeOut();
$("#loginform").css({"visibility":"hidden","display":"none","aria-hidden":"true"});
$("#loginform").attr('aria-hidden', 'true');
}
Accessibility notes:
- The modal has role="dialog" and aria-modal="true". Consider adding a focus trap when open for tighter accessibility.
4) Server side: doLogin (dologin.php)
The original article used the old mysql* API and interpolated POST values directly into SQL — this is insecure and deprecated. Below is an improved example using mysqli with a prepared statement. It assumes a users table with at least columns id and pass. Adapt column names to your schema. If you store hashed passwords use passwordverify(); otherwise, replace the comparison appropriately (but do not store plain-text passwords in production).
<?php
// dologin.php
// Get posted values
$id = isset($_POST['uid']) ? trim($_POST['uid']) : '';
$pass = isset($_POST['upass']) ? $_POST['upass'] : '';
// DB config (use environment variables or config file in production)
$host = 'localhost';
$user = 'root';
$dbpass = ''; // original used empty password
$dbname = 'demo';
// Create mysqli connection
$conn = new mysqli($host, $user, $dbpass, $dbname);
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
}
// Use prepared statement to avoid SQL injection
$stmt = $conn->prepare('SELECT id, pass FROM user WHERE id = ? LIMIT 1');
if (!$stmt) {
die('Prepare failed: ' . $conn->error);
}
$stmt->bind_param('s', $id);
$stmt->execute();
$result = $stmt->get_result();
if ($result && $result->num_rows === 1) {
$row = $result->fetch_assoc();
// If stored passwords are hashed (recommended):
// if (password_verify($pass, $row['pass'])) { ... }
// The original example used plain text comparison; update your DB to use password_hash instead.
if ($pass === $row['pass']) {
// Authentication successful
// session_start();
// $_SESSION['id'] = $id;
echo $id;
echo "Successfully Logged In";
header("Location: ./delete.php");
exit;
} else {
echo "Wrong Id Or Password";
header("Location: ./view_data.php");
exit;
}
} else {
echo "Wrong Id Or Password";
header("Location: ./view_data.php");
exit;
}
$stmt->close();
$conn->close();
?>
Security recommendations (must-haves before using in production):
- Use HTTPS so credentials aren't sent in cleartext.
- Store passwords with passwordhash() and validate with passwordverify().
- Use prepared statements (shown above) to prevent SQL injection.
- Do not echo raw POST values in production; only output error messages or redirect.
- Use session_start() and secure session handling for logged-in state.
Notes and resources
- You can get the close.png from here: https://sites.google.com/site/karjolblogs/downloads/close.png?attredirects=0&d=1
- jQuery: https://jquery.com/ (or use the CDN link in the HTML example)
If the user entered the correct Email Id and password we display a success message and redirect the user; otherwise we display "Wrong Id or Password".
That's it. Thank you. Happy Coding..
No comments:
Post a Comment