autofill survey form
This commit is contained in:
parent
6255e8b900
commit
adc3e210b7
26
admin.php
26
admin.php
@ -7,9 +7,25 @@ if (!isset($_SESSION['user_id']) || !in_array('Admin', $_SESSION['user_roles']))
|
||||
exit;
|
||||
}
|
||||
|
||||
// If logged in, show the admin content
|
||||
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
|
||||
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
||||
$records_per_page = 10;
|
||||
@ -54,7 +70,7 @@ require_once 'templates/header.php';
|
||||
<th>Submitter</th>
|
||||
<th>Email</th>
|
||||
<th>Submitted At</th>
|
||||
<th>Answers</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@ -73,7 +89,11 @@ require_once 'templates/header.php';
|
||||
<td><?= htmlspecialchars($submission['email']) ?></td>
|
||||
<td><?= $submission['created_at'] ?></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>
|
||||
</tr>
|
||||
<?php endforeach;
|
||||
|
||||
@ -58,3 +58,21 @@ a:hover {
|
||||
.hidden {
|
||||
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;
|
||||
}
|
||||
@ -2,6 +2,75 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||
const surveyForm = document.getElementById('survey-form');
|
||||
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 formContainer = document.querySelector('.form-container');
|
||||
|
||||
@ -43,3 +112,4 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
34
survey.php
34
survey.php
@ -9,6 +9,24 @@ if (!isset($_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
|
||||
$survey_stmt = db()->prepare("SELECT * FROM surveys WHERE id = ?");
|
||||
$survey_stmt->execute([$survey_id]);
|
||||
@ -53,16 +71,23 @@ require_once 'templates/header.php';
|
||||
<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="<?= isset($_GET['email']) ? htmlspecialchars($_GET['email']) : '' ?>" disabled required>
|
||||
<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'): ?>
|
||||
@ -89,9 +114,14 @@ require_once 'templates/header.php';
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?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>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -24,20 +24,25 @@ $isSurveyPage = basename($_SERVER['PHP_SELF']) == 'survey.php';
|
||||
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container">
|
||||
<div class="d-flex align-items-center">
|
||||
<a class="navbar-brand" href="https://flatlogic.com/">
|
||||
<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">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav ml-auto">
|
||||
<?php if (!$isSurveyPage): ?>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="index.php">Home</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="surveys.php">Surveys</a>
|
||||
</li>
|
||||
<?php endif; ?>
|
||||
<?php if (isset($_SESSION['user_id'])): ?>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="dashboard.php">Dashboard</a>
|
||||
@ -45,8 +50,6 @@ $isSurveyPage = basename($_SERVER['PHP_SELF']) == 'survey.php';
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="logout.php">Logout</a>
|
||||
</li>
|
||||
<?php else: ?>
|
||||
|
||||
<?php endif; ?>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user