112 lines
5.3 KiB
PHP
112 lines
5.3 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db/config.php';
|
|
require_once __DIR__ . '/includes/security.php';
|
|
|
|
$survey_id = $_GET['id'] ?? null;
|
|
$error_message = '';
|
|
$survey = null;
|
|
$questions = [];
|
|
|
|
if (!$survey_id) {
|
|
$error_message = "No survey specified. Please provide a survey ID.";
|
|
} else {
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Fetch survey details
|
|
$survey_stmt = $pdo->prepare("SELECT * FROM surveys WHERE id = ?");
|
|
$survey_stmt->execute([$survey_id]);
|
|
$survey = $survey_stmt->fetch();
|
|
|
|
if (!$survey) {
|
|
$error_message = "The requested survey could not be found.";
|
|
} else {
|
|
// Fetch questions and their options
|
|
$questions_stmt = $pdo->prepare("SELECT * FROM questions WHERE survey_id = ? ORDER BY id ASC");
|
|
$questions_stmt->execute([$survey_id]);
|
|
$questions = $questions_stmt->fetchAll();
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error_message = "A database error occurred.";
|
|
// In a real app, you would log the detailed error: error_log($e->getMessage());
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?php echo $survey ? htmlspecialchars($survey['title']) : 'Survey'; ?> - FormFlex Pro</title>
|
|
<meta name="description" content="<?php echo $survey ? htmlspecialchars($survey['description']) : 'Please complete the survey.'; ?>">
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body>
|
|
|
|
<div class="survey-container">
|
|
<?php if ($error_message): ?>
|
|
<div class="alert alert-danger text-center"><?php echo $error_message; ?></div>
|
|
<?php elseif ($survey): ?>
|
|
<div class="survey-header">
|
|
<h1><?php echo htmlspecialchars($survey['title']); ?></h1>
|
|
<p class="lead mb-0"><?php echo htmlspecialchars($survey['description']); ?></p>
|
|
</div>
|
|
|
|
<form id="surveyForm" action="survey_submit.php" method="POST">
|
|
<?php echo csrf_input_field(); ?>
|
|
<input type="hidden" name="survey_id" value="<?php echo htmlspecialchars($survey_id); ?>">
|
|
|
|
<?php foreach ($questions as $index => $question): ?>
|
|
<div class="question-card">
|
|
<label class="form-label"><?php echo ($index + 1) . ". " . htmlspecialchars($question['question_text']); ?></label>
|
|
<?php
|
|
$q_id = $question['id'];
|
|
$q_type = $question['question_type'];
|
|
$input_name = "answers[" . $q_id . "]";
|
|
|
|
if ($q_type == 'text') {
|
|
echo "<input type='text' class='form-control' name='{$input_name}'>";
|
|
} elseif ($q_type == 'textarea') {
|
|
echo "<textarea class='form-control' name='{$input_name}' rows='4'></textarea>";
|
|
} elseif (in_array($q_type, ['radio', 'checkbox', 'select'])) {
|
|
$options_stmt = $pdo->prepare("SELECT * FROM question_options WHERE question_id = ? ORDER BY id ASC");
|
|
$options_stmt->execute([$q_id]);
|
|
$options = $options_stmt->fetchAll();
|
|
|
|
if ($q_type == 'select') {
|
|
echo "<select class='form-select' name='{$input_name}'>";
|
|
echo "<option value='' selected disabled>-- Please select --</option>";
|
|
foreach ($options as $option) {
|
|
echo "<option value='" . htmlspecialchars($option['option_text']) . "'>" . htmlspecialchars($option['option_text']) . "</option>";
|
|
}
|
|
echo "</select>";
|
|
} else { // radio or checkbox
|
|
$input_type = $q_type;
|
|
$name_attr = ($q_type == 'checkbox') ? $input_name . "[]" : $input_name;
|
|
foreach ($options as $opt_index => $option) {
|
|
$option_id = "q{$q_id}_opt{$opt_index}";
|
|
echo "<div class='form-check'>";
|
|
echo "<input class='form-check-input' type='{$input_type}' name='{$name_attr}' id='{$option_id}' value='" . htmlspecialchars($option['option_text']) . "'>";
|
|
echo "<label class='form-check-label' for='{$option_id}'>" . htmlspecialchars($option['option_text']) . "</label>";
|
|
echo "</div>";
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
|
|
<div class="survey-footer">
|
|
<button type="submit" class="btn btn-primary">Submit Survey</button>
|
|
<div class="powered-by">
|
|
Powered by <a href="index.php" target="_blank">FormFlex Pro</a>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|