36695-vm/donor_registration.php
2025-12-07 18:31:47 +00:00

209 lines
8.4 KiB
PHP

<?php
$pageTitle = "Donor Registration";
$pageDescription = "Register as an organ donor.";
$successMessage = "";
$errorMessage = "";
// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
require_once 'db/config.php';
try {
$pdo = db();
// Idempotent table creation
$pdo->exec("
CREATE TABLE IF NOT EXISTS donors (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INT NOT NULL,
blood_type VARCHAR(3) NOT NULL,
weight_kg FLOAT NOT NULL,
phone VARCHAR(20) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
emergency_contact_name VARCHAR(255) NOT NULL,
emergency_contact_phone VARCHAR(20) NOT NULL,
status VARCHAR(20) DEFAULT 'Pending Verification',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
");
// Validation
$required_fields = ['name', 'age', 'blood_type', 'weight_kg', 'phone', 'email', 'emergency_contact_name', 'emergency_contact_phone'];
foreach ($required_fields as $field) {
if (empty($_POST[$field])) {
throw new Exception("All fields are required.");
}
}
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
throw new Exception("Invalid email format.");
}
if ($_POST['age'] < 18 || $_POST['age'] > 75) {
throw new Exception("Age must be between 18 and 75.");
}
// Insert data
$stmt = $pdo->prepare(
"INSERT INTO donors (name, age, blood_type, weight_kg, phone, email, emergency_contact_name, emergency_contact_phone) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
);
$stmt->execute([
$_POST['name'],
$_POST['age'],
$_POST['blood_type'],
$_POST['weight_kg'],
$_POST['phone'],
$_POST['email'],
$_POST['emergency_contact_name'],
$_POST['emergency_contact_phone']
]);
$successMessage = "Thank you for registering! Your application is pending verification.";
} catch (PDOException $e) {
if ($e->errorInfo[1] == 1062) { // Duplicate entry for email
$errorMessage = "This email address is already registered.";
} else {
$errorMessage = "Database error: " . $e->getMessage();
}
} catch (Exception $e) {
$errorMessage = $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= htmlspecialchars($pageTitle) ?> - Organ Donation System</title>
<meta name="description" content="<?= htmlspecialchars($pageDescription) ?>">
<!-- Bootstrap 5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
<style>
body {
background-color: #f8f9fa;
}
.form-container {
max-width: 800px;
margin: 50px auto;
background: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}
</style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="index.php">
<i class="bi bi-heart-pulse-fill text-primary"></i>
Organ Donation
</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="form-container">
<h2 class="text-center mb-4">Donor Registration</h2>
<p class="text-center text-muted mb-4">Complete the form below to become a registered organ donor.</p>
<?php if ($successMessage): ?>
<div class="alert alert-success">
<i class="bi bi-check-circle-fill"></i>
<?= htmlspecialchars($successMessage) ?>
</div>
<?php endif; ?>
<?php if ($errorMessage): ?>
<div class="alert alert-danger">
<i class="bi bi-exclamation-triangle-fill"></i>
<?= htmlspecialchars($errorMessage) ?>
</div>
<?php endif; ?>
<?php if (!$successMessage): ?>
<form action="donor_registration.php" method="POST" novalidate>
<div class="row">
<div class="col-md-6 mb-3">
<label for="name" class="form-label">Full Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="col-md-6 mb-3">
<label for="email" class="form-label">Email Address</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
</div>
<div class="row">
<div class="col-md-4 mb-3">
<label for="age" class="form-label">Age (18-75)</label>
<input type="number" class="form-control" id="age" name="age" min="18" max="75" required>
</div>
<div class="col-md-4 mb-3">
<label for="blood_type" class="form-label">Blood Type</label>
<select class="form-select" id="blood_type" name="blood_type" required>
<option value="">Select...</option>
<option value="A+">A+</option>
<option value="A-">A-</option>
<option value="B+">B+</option>
<option value="B-">B-</option>
<option value="AB+">AB+</option>
<option value="AB-">AB-</option>
<option value="O+">O+</option>
<option value="O-">O-</option>
</select>
</div>
<div class="col-md-4 mb-3">
<label for="weight_kg" class="form-label">Weight (kg)</label>
<input type="number" step="0.1" class="form-control" id="weight_kg" name="weight_kg" required>
</div>
</div>
<div class="mb-3">
<label for="phone" class="form-label">Phone Number</label>
<input type="tel" class="form-control" id="phone" name="phone" required>
</div>
<hr class="my-4">
<h5 class="mb-3">Emergency Contact</h5>
<div class="row">
<div class="col-md-6 mb-3">
<label for="emergency_contact_name" class="form-label">Contact Name</label>
<input type="text" class="form-control" id="emergency_contact_name" name="emergency_contact_name" required>
</div>
<div class="col-md-6 mb-3">
<label for="emergency_contact_phone" class="form-label">Contact Phone</label>
<input type="tel" class="form-control" id="emergency_contact_phone" name="emergency_contact_phone" required>
</div>
</div>
<div class="d-grid mt-4">
<button type="submit" class="btn btn-primary btn-lg">Submit Registration</button>
</div>
</form>
<?php endif; ?>
</div>
</div>
<footer class="text-center py-4 text-muted">
<p>&copy; <?= date("Y") ?> Organ Donation System. All rights reserved.</p>
</footer>
<!-- Bootstrap 5 JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>