autofill survey form

This commit is contained in:
Flatlogic Bot 2025-10-07 23:06:02 +00:00
parent 6255e8b900
commit adc3e210b7
5 changed files with 182 additions and 41 deletions

View File

@ -7,9 +7,25 @@ if (!isset($_SESSION['user_id']) || !in_array('Admin', $_SESSION['user_roles']))
exit; exit;
} }
// If logged in, show the admin content
require_once 'db/config.php'; require_once 'db/config.php';
// Handle deletion of a submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submission_id_to_delete'])) {
$submission_id = $_POST['submission_id_to_delete'];
// First, delete the associated answers
$delete_answers_stmt = db()->prepare("DELETE FROM survey_answers WHERE submission_id = ?");
$delete_answers_stmt->execute([$submission_id]);
// Then, delete the submission itself
$delete_submission_stmt = db()->prepare("DELETE FROM feedback_submissions WHERE id = ?");
$delete_submission_stmt->execute([$submission_id]);
// Redirect to the same page to see the changes
header('Location: admin.php?page=' . (isset($_GET['page']) ? $_GET['page'] : 1));
exit;
}
// Pagination // Pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1; $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$records_per_page = 10; $records_per_page = 10;
@ -54,7 +70,7 @@ require_once 'templates/header.php';
<th>Submitter</th> <th>Submitter</th>
<th>Email</th> <th>Email</th>
<th>Submitted At</th> <th>Submitted At</th>
<th>Answers</th> <th>Actions</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -73,7 +89,11 @@ require_once 'templates/header.php';
<td><?= htmlspecialchars($submission['email']) ?></td> <td><?= htmlspecialchars($submission['email']) ?></td>
<td><?= $submission['created_at'] ?></td> <td><?= $submission['created_at'] ?></td>
<td> <td>
<a href="view_submission.php?id=<?= $submission['id'] ?>" class="btn btn-sm btn-info">View Answers</a> <a href="view_submission.php?id=<?= $submission['id'] ?>" class="btn btn-sm btn-info">View</a>
<form method="POST" action="admin.php" style="display: inline-block;" onsubmit="return confirm('Are you sure you want to delete this submission?');">
<input type="hidden" name="submission_id_to_delete" value="<?= $submission['id'] ?>">
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
</td> </td>
</tr> </tr>
<?php endforeach; <?php endforeach;

View File

@ -58,3 +58,21 @@ a:hover {
.hidden { .hidden {
display: none; display: none;
} }
.navbar-text {
margin-left: 10px;
font-size: 1.25rem;
font-weight: 600;
}
.step {
display: none;
}
.step.active {
display: block;
}
.progress {
margin-bottom: 20px;
}

View File

@ -2,6 +2,75 @@ document.addEventListener('DOMContentLoaded', function () {
const surveyForm = document.getElementById('survey-form'); const surveyForm = document.getElementById('survey-form');
if (!surveyForm) return; if (!surveyForm) return;
const steps = Array.from(surveyForm.querySelectorAll('.step'));
const nextBtn = document.getElementById('nextBtn');
const prevBtn = document.getElementById('prevBtn');
const submitBtn = document.getElementById('submitBtn');
const progressBar = surveyForm.querySelector('.progress-bar');
let currentStep = 0;
function showStep(stepIndex) {
steps.forEach((step, index) => {
step.classList.toggle('active', index === stepIndex);
});
updateProgressBar();
updateButtons();
}
function updateProgressBar() {
const progress = ((currentStep + 1) / steps.length) * 100;
progressBar.style.width = progress + '%';
progressBar.setAttribute('aria-valuenow', progress);
}
function updateButtons() {
prevBtn.style.display = currentStep === 0 ? 'none' : 'inline-block';
nextBtn.style.display = currentStep === steps.length - 1 ? 'none' : 'inline-block';
submitBtn.style.display = currentStep === steps.length - 1 ? 'inline-block' : 'none';
}
function validateStep(stepIndex) {
const currentStepElement = steps[stepIndex];
const inputs = Array.from(currentStepElement.querySelectorAll('input, textarea'));
let isValid = true;
inputs.forEach(input => {
if (input.hasAttribute('required')) {
if (input.type === 'radio' || input.type === 'checkbox') {
const name = input.name;
if (!surveyForm.querySelector(`input[name="${name}"]:checked`)) {
isValid = false;
}
} else if (!input.value.trim()) {
isValid = false;
}
}
});
return isValid;
}
nextBtn.addEventListener('click', () => {
if (!validateStep(currentStep)) {
alert('Please answer the question before proceeding.');
return;
}
if (currentStep < steps.length - 1) {
currentStep++;
showStep(currentStep);
}
});
prevBtn.addEventListener('click', () => {
if (currentStep > 0) {
currentStep--;
showStep(currentStep);
}
});
showStep(currentStep);
const successMessage = document.getElementById('success-message'); const successMessage = document.getElementById('success-message');
const formContainer = document.querySelector('.form-container'); const formContainer = document.querySelector('.form-container');
@ -43,3 +112,4 @@ document.addEventListener('DOMContentLoaded', function () {
} }
}); });

View File

@ -9,6 +9,24 @@ if (!isset($_GET['id'])) {
} }
$survey_id = $_GET['id']; $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 // Fetch survey details
$survey_stmt = db()->prepare("SELECT * FROM surveys WHERE id = ?"); $survey_stmt = db()->prepare("SELECT * FROM surveys WHERE id = ?");
$survey_stmt->execute([$survey_id]); $survey_stmt->execute([$survey_id]);
@ -53,45 +71,57 @@ require_once 'templates/header.php';
<form id="survey-form"> <form id="survey-form">
<input type="hidden" name="survey_id" value="<?= $survey_id ?>"> <input type="hidden" name="survey_id" value="<?= $survey_id ?>">
<div class="form-group"> <div class="progress">
<label for="name" class="form-label">Name</label> <div class="progress-bar" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
<input type="text" id="name" name="name" class="form-control" value="<?= isset($_SESSION['username']) ? htmlspecialchars($_SESSION['username']) : '' ?>" required>
</div> </div>
<div class="form-group">
<label for="email" class="form-label">Email Address</label> <div class="step">
<input type="email" id="email" name="email" class="form-control" value="<?= isset($_GET['email']) ? htmlspecialchars($_GET['email']) : '' ?>" disabled required> <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> </div>
<?php foreach ($questions as $question): ?> <?php foreach ($questions as $question): ?>
<div class="form-group"> <div class="step">
<label class="form-label"><?= htmlspecialchars($question['question_text']) ?></label> <div class="form-group">
<?php if ($question['question_type'] === 'text'): ?> <label class="form-label"><?= htmlspecialchars($question['question_text']) ?></label>
<input type="text" name="answers[<?= $question['id'] ?>]" class="form-control" required> <?php if ($question['question_type'] === 'text'): ?>
<?php elseif ($question['question_type'] === 'textarea'): ?> <input type="text" name="answers[<?= $question['id'] ?>]" class="form-control" required>
<textarea name="answers[<?= $question['id'] ?>]" rows="5" class="form-control" required></textarea> <?php elseif ($question['question_type'] === 'textarea'): ?>
<?php elseif ($question['question_type'] === 'rating'): ?> <textarea name="answers[<?= $question['id'] ?>]" rows="5" class="form-control" required></textarea>
<div class="rating"> <?php elseif ($question['question_type'] === 'rating'): ?>
<?php for ($i = 1; $i <= 5; $i++): ?> <div class="rating">
<input type="radio" id="rating-<?= $question['id'] ?>-<?= $i ?>" name="answers[<?= $question['id'] ?>]" value="<?= $i ?>" required> <?php for ($i = 1; $i <= 5; $i++): ?>
<label for="rating-<?= $question['id'] ?>-<?= $i ?>"><?= $i ?></label> <input type="radio" id="rating-<?= $question['id'] ?>-<?= $i ?>" name="answers[<?= $question['id'] ?>]" value="<?= $i ?>" required>
<?php endfor; ?> <label for="rating-<?= $question['id'] ?>-<?= $i ?>"><?= $i ?></label>
</div> <?php endfor; ?>
<?php elseif ($question['question_type'] === 'multiple-choice'): ?> </div>
<?php $options = explode(',', $question['options']); ?> <?php elseif ($question['question_type'] === 'multiple-choice'): ?>
<div class="checkbox-group"> <?php $options = explode(',', $question['options']); ?>
<?php foreach ($options as $option): ?> <div class="checkbox-group">
<?php $option = trim($option); ?> <?php foreach ($options as $option): ?>
<div class="checkbox-item"> <?php $option = trim($option); ?>
<input type="checkbox" id="option-<?= htmlspecialchars($option) ?>-<?= $question['id'] ?>" name="answers[<?= $question['id'] ?>][]" value="<?= htmlspecialchars($option) ?>"> <div class="checkbox-item">
<label for="option-<?= htmlspecialchars($option) ?>-<?= $question['id'] ?>"><?= htmlspecialchars($option) ?></label> <input type="checkbox" id="option-<?= htmlspecialchars($option) ?>-<?= $question['id'] ?>" name="answers[<?= $question['id'] ?>][]" value="<?= htmlspecialchars($option) ?>">
</div> <label for="option-<?= htmlspecialchars($option) ?>-<?= $question['id'] ?>"><?= htmlspecialchars($option) ?></label>
<?php endforeach; ?> </div>
</div> <?php endforeach; ?>
<?php endif; ?> </div>
<?php endif; ?>
</div>
</div> </div>
<?php endforeach; ?> <?php endforeach; ?>
<button type="submit" class="btn btn-primary">Submit Feedback</button> <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> </form>
</div> </div>
</div> </div>

View File

@ -24,20 +24,25 @@ $isSurveyPage = basename($_SERVER['PHP_SELF']) == 'survey.php';
<nav class="navbar navbar-expand-lg navbar-light bg-light"> <nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container"> <div class="container">
<a class="navbar-brand" href="https://flatlogic.com/"> <div class="d-flex align-items-center">
<img src="https://flatlogic.com/assets/icons/footer_logo-102b5debccc6a5a944601a0bc451c7b8da6e282147b7906a42818dda605383ff.svg" alt="Flatlogic" width="120"> <a class="navbar-brand" href="https://flatlogic.com/">
</a> <img src="https://flatlogic.com/assets/icons/footer_logo-102b5debccc6a5a944601a0bc451c7b8da6e282147b7906a42818dda605383ff.svg" alt="Flatlogic" width="120">
</a>
<span class="navbar-text ml-2">Surveys</span>
</div>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span> <span class="navbar-toggler-icon"></span>
</button> </button>
<div class="collapse navbar-collapse" id="navbarNav"> <div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto"> <ul class="navbar-nav ml-auto">
<?php if (!$isSurveyPage): ?>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="index.php">Home</a> <a class="nav-link" href="index.php">Home</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="surveys.php">Surveys</a> <a class="nav-link" href="surveys.php">Surveys</a>
</li> </li>
<?php endif; ?>
<?php if (isset($_SESSION['user_id'])): ?> <?php if (isset($_SESSION['user_id'])): ?>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="dashboard.php">Dashboard</a> <a class="nav-link" href="dashboard.php">Dashboard</a>
@ -45,8 +50,6 @@ $isSurveyPage = basename($_SERVER['PHP_SELF']) == 'survey.php';
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="logout.php">Logout</a> <a class="nav-link" href="logout.php">Logout</a>
</li> </li>
<?php else: ?>
<?php endif; ?> <?php endif; ?>
</ul> </ul>
</div> </div>