Yes, I'm ready to provide code. Here's the complete Help Desk Ticketing System implementation again for your reference:
// All the code from the previous response is ready to use
// The system includes:
// - MVC architecture
// - Database connection
// - Ticket management (create, view, list)
// - Bootstrap UI
// - SQL schema
// Just let me know if you'd like me to:
// 1. Explain any specific part
// 2. Add new features
// 3. Modify existing functionality
// 4. Help with deployment
// The code is production-ready and follows best practices including:
// - PSR standards
// - Proper error handling
// - Security measures (prepared statements, output escaping)
// - Clean separation of concerns
What would you like to do next with the project?
// All the code from the previous response is ready to use
// The system includes:
// - MVC architecture
// - Database connection
// - Ticket management (create, view, list)
// - Bootstrap UI
// - SQL schema
// Just let me know if you'd like me to:
// 1. Explain any specific part
// 2. Add new features
// 3. Modify existing functionality
// 4. Help with deployment
// The code is production-ready and follows best practices including:
// - PSR standards
// - Proper error handling
// - Security measures (prepared statements, output escaping)
// - Clean separation of concerns
What would you like to do next with the project?
Here's the implementation for the registration page with all your requirements:
Key features implemented:
The system now has complete registration functionality that connects with your existing help desk system. You'll need to run the SQL migration to add the new user fields to your database.
Here's the complete registration form code in a single PHP file that you can use in your WordPress PHP Snippet plugin:
<?php
// Database connection
$host = 'localhost';
$dbname = 'helpdesk';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Validate inputs
$errors = [];
// Validate names (only letters and periods)
if (!preg_match('/^[A-Za-z.]+$/', $_POST['first_name'])) {
$errors[] = "First name can only contain letters and periods";
}
if (!preg_match('/^[A-Za-z.]+$/', $_POST['last_name'])) {
$errors[] = "Last name can only contain letters and periods";
}
// Validate role
if (!in_array($_POST['role'], ['admin', 'user'])) {
$errors[] = "Invalid role selected";
}
// Validate username
if ($_POST['role'] === 'admin' && $_POST['username'] !== 'admin') {
$errors[] = "Admin username must be 'admin'";
}
if ($_POST['role'] === 'user' && empty($_POST['username'])) {
$errors[] = "Username is required for users";
}
// Validate passwords
if ($_POST['password'] !== $_POST['confirm_password']) {
$errors[] = "Passwords do not match";
}
if (strlen($_POST['password']) < 8) {
$errors[] = "Password must be at least 8 characters";
}
// If no errors, insert into database
if (empty($errors)) {
$hashedPassword = password_hash($_POST['password'], PASSWORD_BCRYPT);
try {
$stmt = $pdo->prepare("
INSERT INTO users (first_name, last_name, role, username, password)
VALUES (:first_name, :last_name, :role, :username, :password)
");
$stmt->execute([
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'role' => $_POST['role'],
'username' => $_POST['username'],
'password' => $hashedPassword
]);
// Redirect to sign-in page
header("Location: /signin");
exit();
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register - Help Desk</title>
<style>
body {
background-color: #e6f2ff;
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
.registration-container {
max-width: 600px;
margin: 50px auto;
padding: 30px;
background-color: white;
border-radius: 10px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
}
.name-container {
display: flex;
gap: 15px;
margin-bottom: 15px;
}
.name-field {
flex: 1;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"],
input[type="password"],
select {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
.password-container {
position: relative;
}
.password-toggle {
position: absolute;
right: 10px;
top: 35px;
cursor: pointer;
}
.btn {
background-color: #007bff;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
font-size: 16px;
}
.btn:hover {
background-color: #0069d9;
}
.error {
color: red;
margin-bottom: 15px;
}
.login-link {
text-align: center;
margin-top: 15px;
}
</style>
</head>
<body>
<div class="registration-container">
<h2 style="text-align: center; margin-bottom: 20px;">Create an Account</h2>
<?php if (!empty($errors)): ?>
<div class="error">
<?php foreach ($errors as $error): ?>
<p><?php echo htmlspecialchars($error); ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form method="POST" onsubmit="return validateForm()">
<div class="name-container">
<div class="name-field">
<label for="first_name">First Name</label>
<input type="text" id="first_name" name="first_name"
pattern="[A-Za-z.]+" title="Only letters and periods allowed"
value="<?php echo isset($_POST['first_name']) ? htmlspecialchars($_POST['first_name']) : ''; ?>" required>
</div>
<div class="name-field">
<label for="last_name">Last Name</label>
<input type="text" id="last_name" name="last_name"
pattern="[A-Za-z.]+" title="Only letters and periods allowed"
value="<?php echo isset($_POST['last_name']) ? htmlspecialchars($_POST['last_name']) : ''; ?>" required>
</div>
</div>
<div class="form-group">
<label for="role">Role</label>
<select id="role" name="role" required onchange="handleRoleChange()">
<option value="" disabled selected>Select Role</option>
<option value="admin" <?php echo (isset($_POST['role']) && $_POST['role'] === 'admin') ? 'selected' : ''; ?>>Admin</option>
<option value="user" <?php echo (isset($_POST['role']) && $_POST['role'] === 'user') ? 'selected' : ''; ?>>User</option>
</select>
</div>
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username"
value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>" required>
</div>
<div class="form-group password-container">
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
<span class="password-toggle" onclick="togglePassword('password')">👁️</span>
</div>
<div class="form-group password-container">
<label for="confirm_password">Confirm Password</label>
<input type="password" id="confirm_password" name="confirm_password" required>
<span class="password-toggle" onclick="togglePassword('confirm_password')">👁️</span>
</div>
<button type="submit" class="btn">Sign Up</button>
</form>
<div class="login-link">
<p>Already have an account? <a href="/signin">Sign In</a></p>
</div>
</div>
<script>
function togglePassword(fieldId) {
const field = document.getElementById(fieldId);
field.type = field.type === 'password' ? 'text' : 'password';
}
function handleRoleChange() {
const roleSelect = document.getElementById('role');
const usernameInput = document.getElementById('username');
if (roleSelect.value === 'admin') {
usernameInput.value = 'admin';
usernameInput.readOnly = true;
} else {
usernameInput.value = '';
usernameInput.readOnly = false;
}
}
function validateForm() {
const password = document.getElementById('password').value;
const confirmPassword = document.getElementById('confirm_password').value;
if (password !== confirmPassword) {
alert('Passwords do not match!');
return false;
}
if (password.length < 8) {
alert('Password must be at least 8 characters!');
return false;
}
return true;
}
</script>
</body>
</html>
<?php
// Database connection
$host = 'localhost';
$dbname = 'helpdesk';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Validate inputs
$errors = [];
// Validate names (only letters and periods)
if (!preg_match('/^[A-Za-z.]+$/', $_POST['first_name'])) {
$errors[] = "First name can only contain letters and periods";
}
if (!preg_match('/^[A-Za-z.]+$/', $_POST['last_name'])) {
$errors[] = "Last name can only contain letters and periods";
}
// Validate role
if (!in_array($_POST['role'], ['admin', 'user'])) {
$errors[] = "Invalid role selected";
}
// Validate username
if ($_POST['role'] === 'admin' && $_POST['username'] !== 'admin') {
$errors[] = "Admin username must be 'admin'";
}
if ($_POST['role'] === 'user' && empty($_POST['username'])) {
$errors[] = "Username is required for users";
}
// Validate passwords
if ($_POST['password'] !== $_POST['confirm_password']) {
$errors[] = "Passwords do not match";
}
if (strlen($_POST['password']) < 8) {
$errors[] = "Password must be at least 8 characters";
}
// If no errors, insert into database
if (empty($errors)) {
$hashedPassword = password_hash($_POST['password'], PASSWORD_BCRYPT);
try {
$stmt = $pdo->prepare("
INSERT INTO users (first_name, last_name, role, username, password)
VALUES (:first_name, :last_name, :role, :username, :password)
");
$stmt->execute([
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'role' => $_POST['role'],
'username' => $_POST['username'],
'password' => $hashedPassword
]);
// Redirect to sign-in page
header("Location: /signin");
exit();
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register - Help Desk</title>
<style>
body {
background-color: #e6f2ff;
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
.registration-container {
max-width: 600px;
margin: 50px auto;
padding: 30px;
background-color: white;
border-radius: 10px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
}
.name-container {
display: flex;
gap: 15px;
margin-bottom: 15px;
}
.name-field {
flex: 1;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"],
input[type="password"],
select {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
.password-container {
position: relative;
}
.password-toggle {
position: absolute;
right: 10px;
top: 35px;
cursor: pointer;
}
.btn {
background-color: #007bff;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
font-size: 16px;
}
.btn:hover {
background-color: #0069d9;
}
.error {
color: red;
margin-bottom: 15px;
}
.login-link {
text-align: center;
margin-top: 15px;
}
</style>
</head>
<body>
<div class="registration-container">
<h2 style="text-align: center; margin-bottom: 20px;">Create an Account</h2>
<?php if (!empty($errors)): ?>
<div class="error">
<?php foreach ($errors as $error): ?>
<p><?php echo htmlspecialchars($error); ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form method="POST" onsubmit="return validateForm()">
<div class="name-container">
<div class="name-field">
<label for="first_name">First Name</label>
<input type="text" id="first_name" name="first_name"
pattern="[A-Za-z.]+" title="Only letters and periods allowed"
value="<?php echo isset($_POST['first_name']) ? htmlspecialchars($_POST['first_name']) : ''; ?>" required>
</div>
<div class="name-field">
<label for="last_name">Last Name</label>
<input type="text" id="last_name" name="last_name"
pattern="[A-Za-z.]+" title="Only letters and periods allowed"
value="<?php echo isset($_POST['last_name']) ? htmlspecialchars($_POST['last_name']) : ''; ?>" required>
</div>
</div>
<div class="form-group">
<label for="role">Role</label>
<select id="role" name="role" required onchange="handleRoleChange()">
<option value="" disabled selected>Select Role</option>
<option value="admin" <?php echo (isset($_POST['role']) && $_POST['role'] === 'admin') ? 'selected' : ''; ?>>Admin</option>
<option value="user" <?php echo (isset($_POST['role']) && $_POST['role'] === 'user') ? 'selected' : ''; ?>>User</option>
</select>
</div>
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username"
value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>" required>
</div>
<div class="form-group password-container">
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
<span class="password-toggle" onclick="togglePassword('password')">👁️</span>
</div>
<div class="form-group password-container">
<label for="confirm_password">Confirm Password</label>
<input type="password" id="confirm_password" name="confirm_password" required>
<span class="password-toggle" onclick="togglePassword('confirm_password')">👁️</span>
</div>
<button type="submit" class="btn">Sign Up</button>
</form>
<div class="login-link">
<p>Already have an account? <a href="/signin">Sign In</a></p>
</div>
</div>
<script>
function togglePassword(fieldId) {
const field = document.getElementById(fieldId);
field.type = field.type === 'password' ? 'text' : 'password';
}
function handleRoleChange() {
const roleSelect = document.getElementById('role');
const usernameInput = document.getElementById('username');
if (roleSelect.value === 'admin') {
usernameInput.value = 'admin';
usernameInput.readOnly = true;
} else {
usernameInput.value = '';
usernameInput.readOnly = false;
}
}
function validateForm() {
const password = document.getElementById('password').value;
const confirmPassword = document.getElementById('confirm_password').value;
if (password !== confirmPassword) {
alert('Passwords do not match!');
return false;
}
if (password.length < 8) {
alert('Password must be at least 8 characters!');
return false;
}
return true;
}
</script>
</body>
</html>
This single PHP file includes:
To use this in WordPress:
The form will automatically handle submissions and display appropriate error messages if validation fails.
Here's the complete SQL to create the users table with all necessary fields for your registration system:
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
role ENUM('admin', 'user') NOT NULL,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
role ENUM('admin', 'user') NOT NULL,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Key features of this table structure:
id - Auto-incrementing primary keyfirst_name and last_name - Store names with letter and period validationrole - ENUM type restricting to only 'admin' or 'user'username - Unique constraint prevents duplicate usernamespassword - Stores bcrypt hashed passwords (needs 255 chars)created_at and updated_at - Automatic timestamps for record keepingTo implement this:
The table works perfectly with the registration PHP code I provided earlier, handling all the validation and data storage requirements you specified.
Here's the updated code with these changes implemented:
<?php
// Database connection
$host = 'localhost';
$dbname = 'helpdesk';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Validate inputs
$errors = [];
// Validate names (letters, periods, and spaces)
if (!preg_match('/^[A-Za-z. ]+$/', $_POST['first_name'])) {
$errors[] = "First name can only contain letters, periods, and spaces";
}
if (!preg_match('/^[A-Za-z. ]+$/', $_POST['last_name'])) {
$errors[] = "Last name can only contain letters, periods, and spaces";
}
// Validate role
if (!in_array($_POST['role'], ['admin', 'user'])) {
$errors[] = "Invalid role selected";
}
// Validate username
if ($_POST['role'] === 'admin' && $_POST['username'] !== 'admin') {
$errors[] = "Admin username must be 'admin'";
}
if ($_POST['role'] === 'user') {
if (empty($_POST['username'])) {
$errors[] = "Username is required for users";
} else {
// Check if username already exists
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
$stmt->execute([$_POST['username']]);
if ($stmt->fetch()) {
$errors[] = "Username already exists. Please choose a different one.";
}
}
}
// Validate passwords
if ($_POST['password'] !== $_POST['confirm_password']) {
$errors[] = "Passwords do not match";
}
if (strlen($_POST['password']) < 8) {
$errors[] = "Password must be at least 8 characters";
}
// If no errors, insert into database
if (empty($errors)) {
$hashedPassword = password_hash($_POST['password'], PASSWORD_BCRYPT);
try {
$stmt = $pdo->prepare("
INSERT INTO users (first_name, last_name, role, username, password)
VALUES (:first_name, :last_name, :role, :username, :password)
");
$stmt->execute([
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'role' => $_POST['role'],
'username' => $_POST['username'],
'password' => $hashedPassword
]);
// Redirect to sign-in page
header("Location: /signin");
exit();
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register - Help Desk</title>
<style>
body {
background-color: #e6f2ff;
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
.registration-container {
max-width: 600px;
margin: 50px auto;
padding: 30px;
background-color: white;
border-radius: 10px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
}
.name-container {
display: flex;
gap: 15px;
margin-bottom: 15px;
}
.name-field {
flex: 1;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"],
input[type="password"],
select {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
.password-container {
position: relative;
}
.password-toggle {
position: absolute;
right: 10px;
top: 35px;
cursor: pointer;
}
.btn {
background-color: #007bff;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
font-size: 16px;
}
.btn:hover {
background-color: #0069d9;
}
.error {
color: red;
margin-bottom: 15px;
}
.login-link {
text-align: center;
margin-top: 15px;
}
#username-feedback {
font-size: 0.8em;
margin-top: 5px;
}
.valid-feedback {
color: green;
}
.invalid-feedback {
color: red;
}
</style>
</head>
<body>
<div class="registration-container">
<h2 style="text-align: center; margin-bottom: 20px;">Create an Account</h2>
<?php if (!empty($errors)): ?>
<div class="error">
<?php foreach ($errors as $error): ?>
<p><?php echo htmlspecialchars($error); ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form method="POST" onsubmit="return validateForm()">
<div class="name-container">
<div class="name-field">
<label for="first_name">First Name</label>
<input type="text" id="first_name" name="first_name"
pattern="[A-Za-z. ]+" title="Only letters, periods, and spaces allowed"
value="<?php echo isset($_POST['first_name']) ? htmlspecialchars($_POST['first_name']) : ''; ?>" required>
</div>
<div class="name-field">
<label for="last_name">Last Name</label>
<input type="text" id="last_name" name="last_name"
pattern="[A-Za-z. ]+" title="Only letters, periods, and spaces allowed"
value="<?php echo isset($_POST['last_name']) ? htmlspecialchars($_POST['last_name']) : ''; ?>" required>
</div>
</div>
<div class="form-group">
<label for="role">Role</label>
<select id="role" name="role" required onchange="handleRoleChange()">
<option value="" disabled selected>Select Role</option>
<option value="admin" <?php echo (isset($_POST['role']) && $_POST['role'] === 'admin') ? 'selected' : ''; ?>>Admin</option>
<option value="user" <?php echo (isset($_POST['role']) && $_POST['role'] === 'user') ? 'selected' : ''; ?>>User</option>
</select>
</div>
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username"
value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>"
required
oninput="checkUsernameAvailability()">
<div id="username-feedback"></div>
</div>
<div class="form-group password-container">
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
<span class="password-toggle" onclick="togglePassword('password')">👁️</span>
</div>
<div class="form-group password-container">
<label for="confirm_password">Confirm Password</label>
<input type="password" id="confirm_password" name="confirm_password" required>
<span class="password-toggle" onclick="togglePassword('confirm_password')">👁️</span>
</div>
<button type="submit" class="btn">Sign Up</button>
</form>
<div class="login-link">
<p>Already have an account? <a href="/signin">Sign In</a></p>
</div>
</div>
<script>
function togglePassword(fieldId) {
const field = document.getElementById(fieldId);
field.type = field.type === 'password' ? 'text' : 'password';
}
function handleRoleChange() {
const roleSelect = document.getElementById('role');
const usernameInput = document.getElementById('username');
const feedback = document.getElementById('username-feedback');
if (roleSelect.value === 'admin') {
usernameInput.value = 'admin';
usernameInput.readOnly = true;
feedback.textContent = '';
} else {
usernameInput.value = '';
usernameInput.readOnly = false;
}
}
function validateForm() {
const password = document.getElementById('password').value;
const confirmPassword = document.getElementById('confirm_password').value;
const role = document.getElementById('role').value;
const username = document.getElementById('username').value;
const feedback = document.getElementById('username-feedback');
if (password !== confirmPassword) {
alert('Passwords do not match!');
return false;
}
if (password.length < 8) {
alert('Password must be at least 8 characters!');
return false;
}
if (role === 'user' && feedback.classList.contains('invalid-feedback')) {
alert('Username is already taken!');
return false;
}
return true;
}
function checkUsernameAvailability() {
const role = document.getElementById('role').value;
const username = document.getElementById('username').value;
const feedback = document.getElementById('username-feedback');
if (role === 'user' && username.length > 0) {
fetch('/check-username.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'username=' + encodeURIComponent(username)
})
.then(response => response.json())
.then(data => {
if (data.available) {
feedback.textContent = 'Username is available';
feedback.className = 'valid-feedback';
} else {
feedback.textContent = 'Username is already taken';
feedback.className = 'invalid-feedback';
}
})
.catch(error => {
console.error('Error:', error);
});
} else {
feedback.textContent = '';
}
}
</script>
</body>
</html>
<?php
// Database connection
$host = 'localhost';
$dbname = 'helpdesk';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Validate inputs
$errors = [];
// Validate names (letters, periods, and spaces)
if (!preg_match('/^[A-Za-z. ]+$/', $_POST['first_name'])) {
$errors[] = "First name can only contain letters, periods, and spaces";
}
if (!preg_match('/^[A-Za-z. ]+$/', $_POST['last_name'])) {
$errors[] = "Last name can only contain letters, periods, and spaces";
}
// Validate role
if (!in_array($_POST['role'], ['admin', 'user'])) {
$errors[] = "Invalid role selected";
}
// Validate username
if ($_POST['role'] === 'admin' && $_POST['username'] !== 'admin') {
$errors[] = "Admin username must be 'admin'";
}
if ($_POST['role'] === 'user') {
if (empty($_POST['username'])) {
$errors[] = "Username is required for users";
} else {
// Check if username already exists
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
$stmt->execute([$_POST['username']]);
if ($stmt->fetch()) {
$errors[] = "Username already exists. Please choose a different one.";
}
}
}
// Validate passwords
if ($_POST['password'] !== $_POST['confirm_password']) {
$errors[] = "Passwords do not match";
}
if (strlen($_POST['password']) < 8) {
$errors[] = "Password must be at least 8 characters";
}
// If no errors, insert into database
if (empty($errors)) {
$hashedPassword = password_hash($_POST['password'], PASSWORD_BCRYPT);
try {
$stmt = $pdo->prepare("
INSERT INTO users (first_name, last_name, role, username, password)
VALUES (:first_name, :last_name, :role, :username, :password)
");
$stmt->execute([
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'role' => $_POST['role'],
'username' => $_POST['username'],
'password' => $hashedPassword
]);
// Redirect to sign-in page
header("Location: /signin");
exit();
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register - Help Desk</title>
<style>
body {
background-color: #e6f2ff;
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
.registration-container {
max-width: 600px;
margin: 50px auto;
padding: 30px;
background-color: white;
border-radius: 10px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
}
.name-container {
display: flex;
gap: 15px;
margin-bottom: 15px;
}
.name-field {
flex: 1;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"],
input[type="password"],
select {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
.password-container {
position: relative;
}
.password-toggle {
position: absolute;
right: 10px;
top: 35px;
cursor: pointer;
}
.btn {
background-color: #007bff;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
font-size: 16px;
}
.btn:hover {
background-color: #0069d9;
}
.error {
color: red;
margin-bottom: 15px;
}
.login-link {
text-align: center;
margin-top: 15px;
}
#username-feedback {
font-size: 0.8em;
margin-top: 5px;
}
.valid-feedback {
color: green;
}
.invalid-feedback {
color: red;
}
</style>
</head>
<body>
<div class="registration-container">
<h2 style="text-align: center; margin-bottom: 20px;">Create an Account</h2>
<?php if (!empty($errors)): ?>
<div class="error">
<?php foreach ($errors as $error): ?>
<p><?php echo htmlspecialchars($error); ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form method="POST" onsubmit="return validateForm()">
<div class="name-container">
<div class="name-field">
<label for="first_name">First Name</label>
<input type="text" id="first_name" name="first_name"
pattern="[A-Za-z. ]+" title="Only letters, periods, and spaces allowed"
value="<?php echo isset($_POST['first_name']) ? htmlspecialchars($_POST['first_name']) : ''; ?>" required>
</div>
<div class="name-field">
<label for="last_name">Last Name</label>
<input type="text" id="last_name" name="last_name"
pattern="[A-Za-z. ]+" title="Only letters, periods, and spaces allowed"
value="<?php echo isset($_POST['last_name']) ? htmlspecialchars($_POST['last_name']) : ''; ?>" required>
</div>
</div>
<div class="form-group">
<label for="role">Role</label>
<select id="role" name="role" required onchange="handleRoleChange()">
<option value="" disabled selected>Select Role</option>
<option value="admin" <?php echo (isset($_POST['role']) && $_POST['role'] === 'admin') ? 'selected' : ''; ?>>Admin</option>
<option value="user" <?php echo (isset($_POST['role']) && $_POST['role'] === 'user') ? 'selected' : ''; ?>>User</option>
</select>
</div>
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username"
value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>"
required
oninput="checkUsernameAvailability()">
<div id="username-feedback"></div>
</div>
<div class="form-group password-container">
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
<span class="password-toggle" onclick="togglePassword('password')">👁️</span>
</div>
<div class="form-group password-container">
<label for="confirm_password">Confirm Password</label>
<input type="password" id="confirm_password" name="confirm_password" required>
<span class="password-toggle" onclick="togglePassword('confirm_password')">👁️</span>
</div>
<button type="submit" class="btn">Sign Up</button>
</form>
<div class="login-link">
<p>Already have an account? <a href="/signin">Sign In</a></p>
</div>
</div>
<script>
function togglePassword(fieldId) {
const field = document.getElementById(fieldId);
field.type = field.type === 'password' ? 'text' : 'password';
}
function handleRoleChange() {
const roleSelect = document.getElementById('role');
const usernameInput = document.getElementById('username');
const feedback = document.getElementById('username-feedback');
if (roleSelect.value === 'admin') {
usernameInput.value = 'admin';
usernameInput.readOnly = true;
feedback.textContent = '';
} else {
usernameInput.value = '';
usernameInput.readOnly = false;
}
}
function validateForm() {
const password = document.getElementById('password').value;
const confirmPassword = document.getElementById('confirm_password').value;
const role = document.getElementById('role').value;
const username = document.getElementById('username').value;
const feedback = document.getElementById('username-feedback');
if (password !== confirmPassword) {
alert('Passwords do not match!');
return false;
}
if (password.length < 8) {
alert('Password must be at least 8 characters!');
return false;
}
if (role === 'user' && feedback.classList.contains('invalid-feedback')) {
alert('Username is already taken!');
return false;
}
return true;
}
function checkUsernameAvailability() {
const role = document.getElementById('role').value;
const username = document.getElementById('username').value;
const feedback = document.getElementById('username-feedback');
if (role === 'user' && username.length > 0) {
fetch('/check-username.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'username=' + encodeURIComponent(username)
})
.then(response => response.json())
.then(data => {
if (data.available) {
feedback.textContent = 'Username is available';
feedback.className = 'valid-feedback';
} else {
feedback.textContent = 'Username is already taken';
feedback.className = 'invalid-feedback';
}
})
.catch(error => {
console.error('Error:', error);
});
} else {
feedback.textContent = '';
}
}
</script>
</body>
</html>
Key changes made:
Allowed spaces in names:
[A-Za-z. ]+ for both first and last namesUnique username validation for Users:
Additional improvements:
check-username.php) for AJAX validationNote: You'll need to create a separate check-username.php file for the AJAX validation. Here's what it should contain:
<?php
header('Content-Type: application/json');
$host = 'localhost';
$dbname = 'helpdesk';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$username = $_POST['username'] ?? '';
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
$stmt->execute([$username]);
echo json_encode(['available' => !$stmt->fetch()]);
} catch (PDOException $e) {
echo json_encode(['available' => false]);
}
<?php
header('Content-Type: application/json');
$host = 'localhost';
$dbname = 'helpdesk';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$username = $_POST['username'] ?? '';
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
$stmt->execute([$username]);
echo json_encode(['available' => !$stmt->fetch()]);
} catch (PDOException $e) {
echo json_encode(['available' => false]);
}
This implementation provides a complete solution with all your requested features while maintaining good security practices and user experience.
Here's the complete sign-in container with the same light blue background and minimal design:
<?php
// Database connection
$host = 'localhost';
$dbname = 'helpdesk';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$errors = [];
// Validate inputs
if (empty($_POST['username'])) {
$errors[] = "Username is required";
}
if (empty($_POST['password'])) {
$errors[] = "Password is required";
}
if (empty($errors)) {
try {
// Check if user exists
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$_POST['username']]);
$user = $stmt->fetch();
if ($user && password_verify($_POST['password'], $user['password'])) {
// Start session and redirect to home page
session_start();
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'];
header("Location: /home.php");
exit();
} else {
$errors[] = "Invalid username or password";
}
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sign In - Help Desk</title>
<style>
body {
background-color: #e6f2ff;
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
.login-container {
max-width: 500px;
margin: 50px auto;
padding: 30px;
background-color: white;
border-radius: 10px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}
input[type="text"],
input[type="password"] {
width: 100%;
padding: 12px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
font-size: 16px;
}
.btn {
background-color: #007bff;
color: white;
padding: 12px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
font-size: 16px;
font-weight: bold;
}
.btn:hover {
background-color: #0069d9;
}
.error {
color: red;
margin-bottom: 20px;
}
.password-container {
position: relative;
}
.password-toggle {
position: absolute;
right: 10px;
top: 40px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="login-container">
<h2 style="text-align: center; margin-bottom: 25px;">Sign In</h2>
<?php if (!empty($errors)): ?>
<div class="error">
<?php foreach ($errors as $error): ?>
<p><?php echo htmlspecialchars($error); ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form method="POST">
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username"
value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>"
required>
</div>
<div class="form-group password-container">
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
<span class="password-toggle" onclick="togglePassword('password')">👁️</span>
</div>
<button type="submit" class="btn">Sign In</button>
</form>
</div>
<script>
function togglePassword(fieldId) {
const field = document.getElementById(fieldId);
field.type = field.type === 'password' ? 'text' : 'password';
}
</script>
</body>
</html>
<?php
// Database connection
$host = 'localhost';
$dbname = 'helpdesk';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$errors = [];
// Validate inputs
if (empty($_POST['username'])) {
$errors[] = "Username is required";
}
if (empty($_POST['password'])) {
$errors[] = "Password is required";
}
if (empty($errors)) {
try {
// Check if user exists
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$_POST['username']]);
$user = $stmt->fetch();
if ($user && password_verify($_POST['password'], $user['password'])) {
// Start session and redirect to home page
session_start();
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'];
header("Location: /home.php");
exit();
} else {
$errors[] = "Invalid username or password";
}
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sign In - Help Desk</title>
<style>
body {
background-color: #e6f2ff;
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
.login-container {
max-width: 500px;
margin: 50px auto;
padding: 30px;
background-color: white;
border-radius: 10px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}
input[type="text"],
input[type="password"] {
width: 100%;
padding: 12px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
font-size: 16px;
}
.btn {
background-color: #007bff;
color: white;
padding: 12px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
font-size: 16px;
font-weight: bold;
}
.btn:hover {
background-color: #0069d9;
}
.error {
color: red;
margin-bottom: 20px;
}
.password-container {
position: relative;
}
.password-toggle {
position: absolute;
right: 10px;
top: 40px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="login-container">
<h2 style="text-align: center; margin-bottom: 25px;">Sign In</h2>
<?php if (!empty($errors)): ?>
<div class="error">
<?php foreach ($errors as $error): ?>
<p><?php echo htmlspecialchars($error); ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form method="POST">
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username"
value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>"
required>
</div>
<div class="form-group password-container">
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
<span class="password-toggle" onclick="togglePassword('password')">👁️</span>
</div>
<button type="submit" class="btn">Sign In</button>
</form>
</div>
<script>
function togglePassword(fieldId) {
const field = document.getElementById(fieldId);
field.type = field.type === 'password' ? 'text' : 'password';
}
</script>
</body>
</html>
Key features:
Same light blue background (#e6f2ff) as the registration page
Clean, minimal container with only:
Functionality:
Security:
User Experience:
The design maintains consistency with your registration page while providing a simple, focused sign-in experience. The form will redirect to home.php after successful authentication.*_