134 lines
6.2 KiB
PHP
134 lines
6.2 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'];
|
|
|
|
// --- Email Logic ---
|
|
$email = '';
|
|
$isEmailDisabled = false;
|
|
|
|
// 1. Check for email in URL parameter
|
|
if (isset($_GET['email']) && !empty($_GET['email'])) {
|
|
$email = $_GET['email'];
|
|
// Store it in a cookie for 1 year
|
|
setcookie('user_email', $email, time() + (86400 * 365), "/"); // 86400 = 1 day
|
|
$isEmailDisabled = true;
|
|
}
|
|
// 2. If not in URL, check for email in cookie
|
|
else if (isset($_COOKIE['user_email']) && !empty($_COOKIE['user_email'])) {
|
|
$email = $_COOKIE['user_email'];
|
|
$isEmailDisabled = true;
|
|
}
|
|
// 3. Otherwise, the field will be empty and enabled.
|
|
|
|
// 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="progress">
|
|
<div class="progress-bar" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
|
|
</div>
|
|
|
|
<div class="step">
|
|
<div class="form-group">
|
|
<label for="name" class="form-label">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="<?= htmlspecialchars($email) ?>" <?= $isEmailDisabled ? 'disabled' : '' ?> required>
|
|
</div>
|
|
</div>
|
|
|
|
<?php foreach ($questions as $question): ?>
|
|
<div class="step">
|
|
<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']); ?>
|
|
<div class="checkbox-group">
|
|
<?php foreach ($options as $option): ?>
|
|
<?php $option = trim($option); ?>
|
|
<div class="checkbox-item">
|
|
<input type="checkbox" id="option-<?= htmlspecialchars($option) ?>-<?= $question['id'] ?>" name="answers[<?= $question['id'] ?>][]" value="<?= htmlspecialchars($option) ?>">
|
|
<label for="option-<?= htmlspecialchars($option) ?>-<?= $question['id'] ?>"><?= htmlspecialchars($option) ?></label>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
|
|
<div class="d-flex justify-content-between">
|
|
<button type="button" id="prevBtn" class="btn btn-secondary">Previous</button>
|
|
<button type="button" id="nextBtn" class="btn btn-primary">Next</button>
|
|
<button type="submit" id="submitBtn" class="btn btn-primary">Submit Feedback</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
</main>
|
|
|
|
<?php
|
|
require_once 'templates/footer.php';
|
|
?>
|