34988-vm/view_survey.php
2025-10-15 18:14:20 +00:00

130 lines
6.2 KiB
PHP

<?php
require_once 'db/config.php';
$survey_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if (!$survey_id) {
http_response_code(400);
echo "Error: Survey ID is missing or invalid.";
exit;
}
try {
$pdo = db();
// Fetch survey details
$stmt = $pdo->prepare("SELECT title, description FROM surveys WHERE id = ?");
$stmt->execute([$survey_id]);
$survey = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$survey) {
http_response_code(404);
echo "Error: Survey not found.";
exit;
}
// Fetch questions
$stmt = $pdo->prepare("SELECT id, question_text, question_type, options FROM questions WHERE survey_id = ? ORDER BY id ASC");
$stmt->execute([$survey_id]);
$questions = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
http_response_code(500);
echo "Database error: " . htmlspecialchars($e->getMessage());
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($survey['title']); ?> - Survey</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="index.php">SurveyApp</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="create_survey.php">Create Survey</a>
</li>
</ul>
</div>
</div>
</nav>
<main class="container mt-5">
<div class="card">
<div class="card-header">
<h1><?php echo htmlspecialchars($survey['title']); ?></h1>
</div>
<div class="card-body">
<p class="lead"><?php echo nl2br(htmlspecialchars($survey['description'])); ?></p>
<hr>
<form action="save_response.php" method="POST" id="surveyForm">
<input type="hidden" name="survey_id" value="<?php echo $survey_id; ?>">
<?php foreach ($questions as $index => $question): ?>
<?php var_dump($question['question_type']); ?>
<div class="mb-4">
<label for="question_<?php echo $question['id']; ?>" class="form-label fw-bold"><?php echo ($index + 1) . ". " . htmlspecialchars($question['question_text']); ?></label>
<?php if ($question['question_type'] === 'text'): ?>
<input type="text" name="answers[<?php echo $question['id']; ?>]" id="question_<?php echo $question['id']; ?>_text" class="form-control" required>
<?php elseif ($question['question_type'] === 'multiple-choice'): ?>
<?php $options = json_decode($question['options'], true); ?>
<?php if ($options): ?>
<?php foreach ($options as $option): ?>
<div class="form-check">
<input class="form-check-input" type="radio" name="answers[<?php echo $question['id']; ?>]" id="option_<?php echo $question['id'] . '_' . htmlspecialchars($option); ?>" value="<?php echo htmlspecialchars($option); ?>" required>
<label class="form-check-label" for="option_<?php echo $question['id'] . '_' . htmlspecialchars($option); ?>">
<?php echo htmlspecialchars($option); ?>
</label>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php elseif ($question['question_type'] === 'checkboxes'): ?>
<?php $options = json_decode($question['options'], true); ?>
<?php if ($options): ?>
<?php foreach ($options as $option): ?>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="answers[<?php echo $question['id']; ?>][]" id="option_<?php echo $question['id'] . '_' . htmlspecialchars($option); ?>" value="<?php echo htmlspecialchars($option); ?>">
<label class="form-check-label" for="option_<?php echo $question['id'] . '_' . htmlspecialchars($option); ?>">
<?php echo htmlspecialchars($option); ?>
</label>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php endif; ?>
</div>
<?php endforeach; ?>
<button type="submit" class="btn btn-primary">Submit Answers</button>
</form>
</div>
</div>
</main>
<footer class="footer mt-auto py-3 bg-light">
<div class="container text-center">
<span class="text-muted">© 2025 SurveyApp. All rights reserved. | <a href="privacy.php">Privacy Policy</a></span>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>