answer.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. // answer.php - Renders an existing form for answering
  3. $form_id = $_GET['id'] ?? '';
  4. $form_file = __DIR__ . '/data/forms/' . preg_replace('/[^a-zA-Z0-9_-]/', '', $form_id) . '.json';
  5. if (empty($form_id) || !file_exists($form_file)) {
  6. 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>');
  7. }
  8. $form_data = json_decode(file_get_contents($form_file), true);
  9. // If editing a response
  10. $edit_id = $_GET['edit'] ?? null;
  11. $existing_answers = [];
  12. if ($edit_id) {
  13. // Sanitize edit\_id
  14. $safe_edit_id = preg_replace('/[^a-zA-Z0-9_-]/', '', $edit_id);
  15. $answer_file = __DIR__ . "/data/answers/{$form_id}_{$safe_edit_id}.json";
  16. if (file_exists($answer_file)) {
  17. $answer_data = json_decode(file_get_contents($answer_file), true);
  18. $existing_answers = $answer_data['answers'] ?? [];
  19. }
  20. }
  21. ?>
  22. <!DOCTYPE html>
  23. <html lang="en">
  24. <head>
  25. <meta charset="UTF-8">
  26. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  27. <title><?= htmlspecialchars($form_data['title']) ?> - Intranet Forms</title>
  28. <link rel="stylesheet" href="assets/css/style.css">
  29. </head>
  30. <body>
  31. <header class="site-header">
  32. <div class="container header-inner">
  33. <div class="brand">
  34. <span class="brand-title"><?= htmlspecialchars($form_data['title']) ?></span>
  35. </div>
  36. </div>
  37. </header>
  38. <main class="container">
  39. <!-- Error tracking container (JS managed) -->
  40. <div id="already-submitted-alert" class="alert alert-warning" style="display:none;">
  41. You have already submitted this form. <a href="submit.php?id=<?= htmlspecialchars($form_id) ?>" style="font-weight:bold;">View your answers</a>
  42. </div>
  43. <form action="submit.php" method="POST" id="answer-form">
  44. <input type="hidden" name="form_id" value="<?= htmlspecialchars($form_id) ?>">
  45. <?php if ($edit_id): ?>
  46. <input type="hidden" name="edit_id" value="<?= htmlspecialchars($edit_id) ?>">
  47. <?php endif; ?>
  48. <div class="panel">
  49. <h1 class="card-title"><?= htmlspecialchars($form_data['title']) ?></h1>
  50. <?php if (!empty($form_data['description'])): ?>
  51. <p style="margin-bottom:1rem; color:var(--brand-muted);"><?= nl2br(htmlspecialchars($form_data['description'])) ?></p>
  52. <?php endif; ?>
  53. <h3 style="border-bottom: 1px solid var(--brand-border); padding-bottom: 0.5rem; margin-bottom: 1rem;">Respondent Details</h3>
  54. <div class="form-group">
  55. <label for="respondent_name">Your Name *</label>
  56. <input type="text" id="respondent_name" name="respondent_name" required>
  57. </div>
  58. <div class="form-group">
  59. <label for="respondent_email">Your Email (Optional, to receive a copy)</label>
  60. <input type="email" id="respondent_email" name="respondent_email">
  61. </div>
  62. </div>
  63. <div class="panel">
  64. <h3 style="border-bottom: 1px solid var(--brand-border); padding-bottom: 0.5rem; margin-bottom: 1rem;">Questions</h3>
  65. <?php foreach ($form_data['questions'] as $q): ?>
  66. <div class="form-group">
  67. <label><?= htmlspecialchars($q['label']) ?> *</label>
  68. <?php
  69. $val = $existing_answers[$q['id']] ?? '';
  70. $is_array_val = is_array($val);
  71. if ($q['type'] === 'textarea'): ?>
  72. <textarea id="<?= htmlspecialchars($q['id']) ?>" name="answers[<?= htmlspecialchars($q['id']) ?>]" rows="4" required><?= htmlspecialchars(is_string($val) ? $val : '') ?></textarea>
  73. <?php elseif ($q['type'] === 'single_choice'): ?>
  74. <div class="options-container" style="display:flex; flex-direction:column; gap:0.5rem; margin-top:0.5rem;">
  75. <?php foreach ($q['options'] as $idx => $opt):
  76. $checked = (is_string($val) && $val === $opt) ? 'checked' : '';
  77. ?>
  78. <label style="font-weight:normal; display:flex; align-items:center; gap:0.5rem;">
  79. <input type="radio" name="answers[<?= htmlspecialchars($q['id']) ?>]" value="<?= htmlspecialchars($opt) ?>" required <?= $checked ?> style="width:auto; margin:0;">
  80. <?= htmlspecialchars($opt) ?>
  81. </label>
  82. <?php endforeach; ?>
  83. </div>
  84. <?php elseif ($q['type'] === 'multiple_choice'): ?>
  85. <div class="options-container" style="display:flex; flex-direction:column; gap:0.5rem; margin-top:0.5rem;">
  86. <?php foreach ($q['options'] as $idx => $opt):
  87. $checked = ($is_array_val && in_array($opt, $val)) ? 'checked' : '';
  88. ?>
  89. <label style="font-weight:normal; display:flex; align-items:center; gap:0.5rem;">
  90. <input type="checkbox" name="answers[<?= htmlspecialchars($q['id']) ?>][]" value="<?= htmlspecialchars($opt) ?>" <?= $checked ?> style="width:auto; margin:0;">
  91. <?= htmlspecialchars($opt) ?>
  92. </label>
  93. <?php endforeach; ?>
  94. </div>
  95. <?php elseif ($q['type'] === 'dropdown'): ?>
  96. <select id="<?= htmlspecialchars($q['id']) ?>" name="answers[<?= htmlspecialchars($q['id']) ?>]" required>
  97. <option value="">-- Please select --</option>
  98. <?php foreach ($q['options'] as $opt):
  99. $selected = (is_string($val) && $val === $opt) ? 'selected' : '';
  100. ?>
  101. <option value="<?= htmlspecialchars($opt) ?>" <?= $selected ?>><?= htmlspecialchars($opt) ?></option>
  102. <?php endforeach; ?>
  103. </select>
  104. <?php else: ?>
  105. <input type="text" id="<?= htmlspecialchars($q['id']) ?>" name="answers[<?= htmlspecialchars($q['id']) ?>]" required value="<?= htmlspecialchars(is_string($val) ? $val : '') ?>">
  106. <?php endif; ?>
  107. </div>
  108. <?php endforeach; ?>
  109. </div>
  110. <div class="panel text-center" style="background:transparent; border:none; box-shadow:none;">
  111. <button type="submit" class="btn btn-block" style="font-size: 1.1rem; padding: 0.75rem;">Submit Answers</button>
  112. </div>
  113. </form>
  114. </main>
  115. <script>
  116. const formId = "<?= htmlspecialchars($form_id) ?>";
  117. const isEditMode = <?= $edit_id ? 'true' : 'false' ?>;
  118. </script>
  119. <script src="assets/js/answer.js"></script>
  120. </body>
  121. </html>