| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- // answer.php - Renders an existing form for answering
- $form_id = $_GET['id'] ?? '';
- $form_file = __DIR__ . '/data/forms/' . preg_replace('/[^a-zA-Z0-9_-]/', '', $form_id) . '.json';
- if (empty($form_id) || !file_exists($form_file)) {
- die('<div style="font-family:sans-serif; text-align:center; padding:50px;"><h2>Form Not Found</h2><p>The link may be invalid or expired.</p></div>');
- }
- $form_data = json_decode(file_get_contents($form_file), true);
- // If editing a response
- $edit_id = $_GET['edit'] ?? null;
- $existing_answers = [];
- if ($edit_id) {
- // Sanitize edit\_id
- $safe_edit_id = preg_replace('/[^a-zA-Z0-9_-]/', '', $edit_id);
- $answer_file = __DIR__ . "/data/answers/{$form_id}_{$safe_edit_id}.json";
- if (file_exists($answer_file)) {
- $answer_data = json_decode(file_get_contents($answer_file), true);
- $existing_answers = $answer_data['answers'] ?? [];
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title><?= htmlspecialchars($form_data['title']) ?> - Intranet Forms</title>
- <link rel="stylesheet" href="assets/css/style.css">
- </head>
- <body>
- <header class="site-header">
- <div class="container header-inner">
- <div class="brand">
- <span class="brand-title"><?= htmlspecialchars($form_data['title']) ?></span>
- </div>
- </div>
- </header>
- <main class="container">
- <!-- Error tracking container (JS managed) -->
- <div id="already-submitted-alert" class="alert alert-warning" style="display:none;">
- You have already submitted this form. <a href="submit.php?id=<?= htmlspecialchars($form_id) ?>" style="font-weight:bold;">View your answers</a>
- </div>
- <form action="submit.php" method="POST" id="answer-form">
- <input type="hidden" name="form_id" value="<?= htmlspecialchars($form_id) ?>">
- <?php if ($edit_id): ?>
- <input type="hidden" name="edit_id" value="<?= htmlspecialchars($edit_id) ?>">
- <?php endif; ?>
- <div class="panel">
- <h1 class="card-title"><?= htmlspecialchars($form_data['title']) ?></h1>
- <?php if (!empty($form_data['description'])): ?>
- <p style="margin-bottom:1rem; color:var(--brand-muted);"><?= nl2br(htmlspecialchars($form_data['description'])) ?></p>
- <?php endif; ?>
-
- <h3 style="border-bottom: 1px solid var(--brand-border); padding-bottom: 0.5rem; margin-bottom: 1rem;">Respondent Details</h3>
-
- <div class="form-group">
- <label for="respondent_name">Your Name *</label>
- <input type="text" id="respondent_name" name="respondent_name" required>
- </div>
-
- <div class="form-group">
- <label for="respondent_email">Your Email (Optional, to receive a copy)</label>
- <input type="email" id="respondent_email" name="respondent_email">
- </div>
- </div>
- <div class="panel">
- <h3 style="border-bottom: 1px solid var(--brand-border); padding-bottom: 0.5rem; margin-bottom: 1rem;">Questions</h3>
-
- <?php foreach ($form_data['questions'] as $q): ?>
- <div class="form-group">
- <label><?= htmlspecialchars($q['label']) ?> *</label>
- <?php
- $val = $existing_answers[$q['id']] ?? '';
- $is_array_val = is_array($val);
-
- if ($q['type'] === 'textarea'): ?>
- <textarea id="<?= htmlspecialchars($q['id']) ?>" name="answers[<?= htmlspecialchars($q['id']) ?>]" rows="4" required><?= htmlspecialchars(is_string($val) ? $val : '') ?></textarea>
-
- <?php elseif ($q['type'] === 'single_choice'): ?>
- <div class="options-container" style="display:flex; flex-direction:column; gap:0.5rem; margin-top:0.5rem;">
- <?php foreach ($q['options'] as $idx => $opt):
- $checked = (is_string($val) && $val === $opt) ? 'checked' : '';
- ?>
- <label style="font-weight:normal; display:flex; align-items:center; gap:0.5rem;">
- <input type="radio" name="answers[<?= htmlspecialchars($q['id']) ?>]" value="<?= htmlspecialchars($opt) ?>" required <?= $checked ?> style="width:auto; margin:0;">
- <?= htmlspecialchars($opt) ?>
- </label>
- <?php endforeach; ?>
- </div>
- <?php elseif ($q['type'] === 'multiple_choice'): ?>
- <div class="options-container" style="display:flex; flex-direction:column; gap:0.5rem; margin-top:0.5rem;">
- <?php foreach ($q['options'] as $idx => $opt):
- $checked = ($is_array_val && in_array($opt, $val)) ? 'checked' : '';
- ?>
- <label style="font-weight:normal; display:flex; align-items:center; gap:0.5rem;">
- <input type="checkbox" name="answers[<?= htmlspecialchars($q['id']) ?>][]" value="<?= htmlspecialchars($opt) ?>" <?= $checked ?> style="width:auto; margin:0;">
- <?= htmlspecialchars($opt) ?>
- </label>
- <?php endforeach; ?>
- </div>
- <?php elseif ($q['type'] === 'dropdown'): ?>
- <select id="<?= htmlspecialchars($q['id']) ?>" name="answers[<?= htmlspecialchars($q['id']) ?>]" required>
- <option value="">-- Please select --</option>
- <?php foreach ($q['options'] as $opt):
- $selected = (is_string($val) && $val === $opt) ? 'selected' : '';
- ?>
- <option value="<?= htmlspecialchars($opt) ?>" <?= $selected ?>><?= htmlspecialchars($opt) ?></option>
- <?php endforeach; ?>
- </select>
- <?php else: ?>
- <input type="text" id="<?= htmlspecialchars($q['id']) ?>" name="answers[<?= htmlspecialchars($q['id']) ?>]" required value="<?= htmlspecialchars(is_string($val) ? $val : '') ?>">
- <?php endif; ?>
- </div>
- <?php endforeach; ?>
- </div>
-
- <div class="panel text-center" style="background:transparent; border:none; box-shadow:none;">
- <button type="submit" class="btn btn-block" style="font-size: 1.1rem; padding: 0.75rem;">Submit Answers</button>
- </div>
- </form>
- </main>
- <script>
- const formId = "<?= htmlspecialchars($form_id) ?>";
- const isEditMode = <?= $edit_id ? 'true' : 'false' ?>;
- </script>
- <script src="assets/js/answer.js"></script>
- </body>
- </html>
|