| Linux in-mum-web1499.main-hosting.eu 5.14.0-503.40.1.el9_5.x86_64 #1 SMP PREEMPT_DYNAMIC Mon May 5 06:06:04 EDT 2025 x86_64 Path : /home/u901718425/domains/task.urbanpillar.in/public_html/dashboard/ |
| Current File : /home/u901718425/domains/task.urbanpillar.in/public_html/dashboard/user.php |
<?php
require_once "../db.php";
include('aside.php');
if (!isset($_SESSION['user_id']) && !isset($_SESSION['role'])) {
header('location: /');
exit;
} else if (isset($_SESSION['role']) && $_SESSION['role'] === 'user') {
header('location: /');
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Admin Dashboard</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined" rel="stylesheet" />
<link href="/style.css" rel="stylesheet" />
<style>
body {
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<div class="row m-0">
<?= aside('User'); ?>
<div class="col vh-100 px-4">
<table class="table table-bordered fs-7" id="userTable">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>Email</th>
<th>Role</th>
<th>Created At</th>
<th>Action</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
<!-- Edit Modal -->
<div class="modal fade" id="editModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<form id="editUserForm" class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Edit User</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" id="edit_id" name="id">
<div class="mb-3">
<label class="form-label">Username</label>
<input type="text" name="user_name" id="edit_user_name" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">Email</label>
<input type="email" name="email" id="edit_email" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">Role</label>
<input type="text" name="role" id="edit_role" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">Password <small>(Leave blank to keep unchanged)</small></label>
<input type="password" name="password" id="edit_password" class="form-control">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Update</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
</div>
</form>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
$(document).ready(function () {
const modal = new bootstrap.Modal(document.getElementById('editModal'));
// Fetch all users
function loadUsers() {
$.get('get_users.php', function (data) {
$('#userTable tbody').html('');
data.forEach(user => {
$('#userTable tbody').append(`
<tr>
<td>${user.id}</td>
<td>${user.user_name}</td>
<td>${user.email}</td>
<td>${user.role}</td>
<td>${user.created_at}</td>
<td><button class="btn btn-sm btn-warning edit-btn" data-user='${JSON.stringify(user)}'>Edit</button></td>
</tr>
`);
});
}, 'json');
}
loadUsers();
// Fill modal form on edit
$(document).on('click', '.edit-btn', function () {
const user = $(this).data('user');
$('#edit_id').val(user.id);
$('#edit_user_name').val(user.user_name);
$('#edit_email').val(user.email);
$('#edit_role').val(user.role);
$('#edit_password').val('');
modal.show();
});
// Handle update
$('#editUserForm').on('submit', function (e) {
e.preventDefault();
$.post('/dashboard/update_user.php', $(this).serialize(), function (res) {
if (res.success) {
modal.hide();
loadUsers();
} else {
console.log('Error: ' + res.message);
}
}, 'json');
});
});
</script>
</body>
</html>