Certainly! Below is the fully updated PHP script for adding a health record, incorporating your new custom CSS styles. The styles have been adjusted to match your provided design, ensuring a modern and cohesive look for the form.
<?php
// File: doctors/add_health_record.php
session_start();
require_once '../db_connect.php';
// Ensure the doctor is logged in
if (!isset($_SESSION['doctor_id'])) {
header("Location: login.php");
exit();
}
$doctor_id = $_SESSION['doctor_id'];
$errors = [];
// Get the patient ID from GET parameters
$patient_id = isset($_GET['patient_id']) ? intval($_GET['patient_id']) : null;
if (!$patient_id) {
echo "Invalid patient ID.";
exit();
}
// Check if the doctor is associated with the patient
$relationship_stmt = $pdo->prepare("
SELECT * FROM doctor_patient
WHERE doctor_id = ? AND patient_id = ?
");
$relationship_stmt->execute([$doctor_id, $patient_id]);
$relationship = $relationship_stmt->fetch();
if (!$relationship) {
echo "You are not authorized to add a health record for this patient.";
exit();
}
// Initialize form variables
$blood_pressure = '';
$weight = '';
$bmi = '';
$cholesterol = '';
$prescription = '';
$treatment_progress = '';
$record_date = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Collect and sanitize inputs
$blood_pressure = trim($_POST['blood_pressure']);
$weight = trim($_POST['weight']);
$bmi = trim($_POST['bmi']);
$cholesterol = trim($_POST['cholesterol']);
$prescription = trim($_POST['prescription']);
$treatment_progress = trim($_POST['treatment_progress']);
$record_date = trim($_POST['record_date']);
// CSRF Protection
if (!isset($_POST['csrf_token']) || $_POST['csrf_token'] !== $_SESSION['csrf_token']) {
$errors[] = "Invalid CSRF token. Please try again.";
}
// Validate inputs
if (empty($blood_pressure)) {
$errors[] = "Blood pressure is required.";
}
if ($weight === '' || !is_numeric($weight) || $weight <= 0) {
$errors[] = "Weight must be a positive number.";
}
if ($bmi === '' || !is_numeric($bmi) || $bmi <= 0) {
$errors[] = "BMI must be a positive number.";
}
if ($cholesterol === '' || !is_numeric($cholesterol) || $cholesterol <= 0) {
$errors[] = "Cholesterol level must be a positive number.";
}
if (empty($prescription)) {
$errors[] = "Prescription is required.";
}
if (empty($treatment_progress)) {
$errors[] = "Treatment progress notes are required.";
}
if (empty($record_date)) {
$errors[] = "Record date is required.";
} else {
// Validate date format (YYYY-MM-DD)
$d = DateTime::createFromFormat('Y-m-d', $record_date);
if (!$d || $d->format('Y-m-d') !== $record_date) {
$errors[] = "Invalid date format for record date.";
}
}
if (empty($errors)) {
// Insert the new health record
$insert_stmt = $pdo->prepare("
INSERT INTO health_records
(patient_id, blood_pressure, weight, bmi, cholesterol, prescription, treatment_progress, record_date)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
");
try {
$insert_stmt->execute([
$patient_id,
$blood_pressure,
$weight,
$bmi,
$cholesterol,
$prescription,
$treatment_progress,
$record_date
]);
// Redirect to patient's view page
header("Location: view_patient.php?id=" . urlencode($patient_id) . "&msg=record_created");
exit();
} catch (PDOException $e) {
// Log the error
error_log("Database Insert Error: " . $e->getMessage());
$errors[] = "Failed to add health record. Please try again.";
}
}
}
// Generate CSRF token
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
$csrf_token = $_SESSION['csrf_token'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Health Record - Patient ID <?= htmlspecialchars($patient_id) ?></title>
<style>
/* Custom CSS for modern design */
body {
background-color: #f8f9fa;
font-family: Arial, sans-serif;
padding: 40px;
margin: 0;
}
.container {
max-width: 800px;
background: #ffffff;
padding: 30px;
margin: auto;
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
color: #4F3F02; /* Brown color */
font-weight: bold;
margin-bottom: 30px;
}
h1, h2, h3 {
font-weight: bold;
}
.btn-primary {
background-color: #D1C287; /* Light golden color */
border-color: #D1C287;
color: #4F3F02; /* Brown color */
font-weight: bold;
padding: 12px 24px;
border-radius: 5px;
cursor: pointer;
border: none;
font-size: 16px;
}
.btn-primary:hover {
background-color: #4F3F02; /* Brown color */
border-color: #D1C287;
color: #fff;
}
.error-messages {
background: #f8d7da;
color: #721c24;
padding: 15px;
border-radius: 5px;
margin-bottom: 20px;
border: 1px solid #f5c6cb;
}
.error-messages ul {
list-style-type: square;
padding-left: 20px;
margin: 0;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
color: #4F3F02; /* Brown color */
}
input[type="text"],
input[type="number"],
input[type="date"],
textarea {
width: 100%;
padding: 12px;
border: 1px solid #D1C287; /* Light golden */
border-radius: 5px;
box-sizing: border-box;
}
textarea {
resize: vertical;
}
.back-link {
display: inline-block;
margin-top: 20px;
text-decoration: none;
color: #D1C287; /* Light golden */
font-weight: bold;
}
.back-link:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h2>Add Health Record for Patient ID <?= htmlspecialchars($patient_id) ?></h2>
<?php if (!empty($errors)): ?>
<div class="error-messages">
<p>Please correct the following error(s):</p>
<ul>
<?php foreach ($errors as $error): ?>
<li><?= htmlspecialchars($error) ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<form method="POST" action="">
<!-- CSRF Token -->
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrf_token) ?>">
<div class="form-group">
<label for="blood_pressure">Blood Pressure:</label>
<input type="text" id="blood_pressure" name="blood_pressure" value="<?= htmlspecialchars($blood_pressure) ?>" required>
</div>
<div class="form-group">
<label for="weight">Weight (kg):</label>
<input type="number" step="0.01" id="weight" name="weight" value="<?= htmlspecialchars($weight) ?>" required min="0">
</div>
<div class="form-group">
<label for="bmi">BMI:</label>
<input type="number" step="0.01" id="bmi" name="bmi" value="<?= htmlspecialchars($bmi) ?>" required min="0">
</div>
<div class="form-group">
<label for="cholesterol">Cholesterol (mg/dL):</label>
<input type="number" step="0.01" id="cholesterol" name="cholesterol" value="<?= htmlspecialchars($cholesterol) ?>" required min="0">
</div>
<div class="form-group">
<label for="prescription">Prescription:</label>
<textarea id="prescription" name="prescription" rows="4" required><?= htmlspecialchars($prescription) ?></textarea>
</div>
<div class="form-group">
<label for="treatment_progress">Treatment Progress Notes:</label>
<textarea id="treatment_progress" name="treatment_progress" rows="4" required><?= htmlspecialchars($treatment_progress) ?></textarea>
</div>
<div class="form-group">
<label for="record_date">Record Date:</label>
<input type="date" id="record_date" name="record_date" value="<?= htmlspecialchars($record_date) ?>" required>
</div>
<input type="submit" value="Add Health Record" class="btn-primary">
</form>
<a href="view_patient.php?id=<?= htmlspecialchars($patient_id) ?>" class="back-link">← Back to Patient Record</a>
</div>
</body>
</html>
Summary of Changes:
-
CSS Replacement:
- The original
<style> block has been replaced with your provided custom CSS to ensure the page adheres to your desired modern design.
- Removed some of the original styles that were overridden by the new styles, such as
.header and adjusted .container as needed.
-
Button Styling:
-
Container Styling:
- The
.container class has been slightly adjusted to match the new design aesthetics, ensuring consistent padding, margin, border-radius, and box-shadow.
-
Typography and Colors:
- Updated colors for headings and labels to match the brown (
#4F3F02) and light golden (#D1C287) color scheme.
- Headings (
h2) are bold and centered to enhance readability and visual hierarchy.
-
Error Messages:
- Maintained the
.error-messages styling to ensure error feedback remains clear and consistent.
-
Back Link Styling:
- The back link utilizes the
.back-link class for styling, ensuring it stands out and is easily accessible.
-
Form Inputs:
- All form inputs and textareas have updated border colors and padding to match the new design, improving the overall user experience.
Additional Recommendations:
-
Responsiveness: While the current design is clean, consider adding media queries to ensure the form looks good on all device sizes.
-
Accessibility: Ensure all form fields have associated labels, and consider adding ARIA attributes if necessary to enhance accessibility for users with assistive technologies.
-
Validation: Although server-side validation is implemented, adding client-side validation can provide immediate feedback to users, enhancing the user experience.
Feel free to further customize the styles or structure to better fit your application's needs!