34760-vm/survey.php
Flatlogic Bot d47e350516 v1
2025-10-07 16:17:37 +00:00

101 lines
4.6 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/db/config.php';
// Get survey ID from the URL
if (!isset($_GET['id'])) {
header("Location: index.php");
exit;
}
$survey_id = $_GET['id'];
// Fetch survey details
$survey_stmt = db()->prepare("SELECT * FROM surveys WHERE id = ?");
$survey_stmt->execute([$survey_id]);
$survey = $survey_stmt->fetch();
// If survey doesn't exist, show an error or redirect
if (!$survey) {
$pageTitle = "Survey Not Found";
require_once 'templates/header.php';
echo "<main><section class='survey-section'><div class='container'><h2>Survey not found.</h2></div></section></main>";
require_once 'templates/footer.php';
exit;
}
// Fetch questions for the survey
$questions_stmt = db()->prepare("SELECT * FROM survey_questions WHERE survey_id = ? ORDER BY created_at ASC");
$questions_stmt->execute([$survey_id]);
$questions = $questions_stmt->fetchAll();
$pageTitle = htmlspecialchars($survey['title']);
$description = htmlspecialchars($survey['description']);
require_once 'templates/header.php';
?>
<main>
<section class="hero">
<div class="container">
<h1><?= htmlspecialchars($survey['title']) ?></h1>
<p><?= htmlspecialchars($survey['description']) ?></p>
</div>
</section>
<section class="survey-section">
<div class="container">
<div id="success-message" class="hidden">
<h2>Thank you!</h2>
<p>Your feedback has been submitted successfully. We appreciate you taking the time to help us improve.</p>
</div>
<div class="form-container">
<form id="survey-form">
<input type="hidden" name="survey_id" value="<?= $survey_id ?>">
<div class="form-group">
<label for="name" class="form-label">Full Name</label>
<input type="text" id="name" name="name" class="form-control" value="<?= isset($_SESSION['username']) ? htmlspecialchars($_SESSION['username']) : '' ?>" required>
</div>
<div class="form-group">
<label for="email" class="form-label">Email Address</label>
<input type="email" id="email" name="email" class="form-control" value="<?= isset($_SESSION['email']) ? htmlspecialchars($_SESSION['email']) : '' ?>" required>
</div>
<?php foreach ($questions as $question): ?>
<div class="form-group">
<label class="form-label"><?= htmlspecialchars($question['question_text']) ?></label>
<?php if ($question['question_type'] === 'text'): ?>
<input type="text" name="answers[<?= $question['id'] ?>]" class="form-control" required>
<?php elseif ($question['question_type'] === 'textarea'): ?>
<textarea name="answers[<?= $question['id'] ?>]" rows="5" class="form-control" required></textarea>
<?php elseif ($question['question_type'] === 'rating'): ?>
<div class="rating">
<?php for ($i = 1; $i <= 5; $i++): ?>
<input type="radio" id="rating-<?= $question['id'] ?>-<?= $i ?>" name="answers[<?= $question['id'] ?>]" value="<?= $i ?>" required>
<label for="rating-<?= $question['id'] ?>-<?= $i ?>"><?= $i ?></label>
<?php endfor; ?>
</div>
<?php elseif ($question['question_type'] === 'multiple-choice'): ?>
<?php $options = explode(',', $question['options']); ?>
<select name="answers[<?= $question['id'] ?>]" class="form-control" required>
<option value="">Select an option</option>
<?php foreach ($options as $option): ?>
<option value="<?= htmlspecialchars(trim($option)) ?>"><?= htmlspecialchars(trim($option)) ?></option>
<?php endforeach; ?>
</select>
<?php endif; ?>
</div>
<?php endforeach; ?>
<button type="submit" class="btn btn-primary">Submit Feedback</button>
</form>
</div>
</div>
</section>
</main>
<?php
require_once 'templates/footer.php';
?>