Survey not found.'); } // Check status if (!$preview) { if ($survey['status'] === 'draft') { http_response_code(403); die('

This survey is not yet published.

'); } if ($survey['status'] === 'closed' || $survey['status'] === 'archived') { die(renderClosed($survey)); } if ($survey['closes_at'] && strtotime($survey['closes_at']) < time()) { die(renderClosed($survey)); } } // Load structure $pages = DB::all("SELECT * FROM survey_pages WHERE survey_id = ? ORDER BY page_number", [$survey['id']]); foreach ($pages as &$p) { $p['questions'] = DB::all("SELECT * FROM survey_questions WHERE page_id = ? AND survey_id = ? ORDER BY order_num", [$p['id'], $survey['id']]); foreach ($p['questions'] as &$q) { $q['options'] = $q['options'] ? json_decode($q['options'], true) : []; $q['settings'] = $q['settings'] ? json_decode($q['settings'], true) : []; $q['logic'] = $q['logic'] ? json_decode($q['logic'], true) : []; } unset($q); // Break reference β€” prevents last $q from corrupting later pages } unset($p); // Break reference β€” prevents last $p from corrupting later pages $settings = $survey['settings'] ? json_decode($survey['settings'], true) : []; $theme = $survey['theme'] ? json_decode($survey['theme'], true) : []; // Handle POST submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && !$preview) { handleSubmission($survey, $pages, $settings); } $primaryColor = $theme['primary_color'] ?? '#e94560'; $bgColor = $theme['bg_color'] ?? '#f4f6fb'; $logoUrl = $theme['logo_url'] ?? ''; $removeBrand = !empty($theme['remove_branding']); $showProgress = !isset($settings['show_progress_bar']) || $settings['show_progress_bar']; $allowBack = !isset($settings['allow_back']) || $settings['allow_back']; $totalQ = array_sum(array_map(fn($p) => count($p['questions']), $pages)); function handleSubmission($survey, $pages, $settings) { $respToken = bin2hex(random_bytes(32)); $respId = DB::insert( "INSERT INTO survey_responses (survey_id, respondent_token, status, ip_address, user_agent, device_type) VALUES (?,?,?,?,?,?)", [$survey['id'], $respToken, 'complete', getClientIP(), $_SERVER['HTTP_USER_AGENT'] ?? '', deviceType()] ); foreach ($pages as $page) { foreach ($page['questions'] as $q) { $key = 'q_' . $q['id']; $val = $_POST[$key] ?? null; if ($val === null) continue; $answerText = null; $answerVal = null; $answerJson = null; if (is_array($val)) { $answerJson = json_encode($val); } elseif (is_numeric($val)) { $answerVal = (float)$val; } else { $answerText = substr(strip_tags((string)$val), 0, 10000); } DB::insert( "INSERT INTO survey_answers (response_id, question_id, survey_id, answer_text, answer_value, answer_json) VALUES (?,?,?,?,?,?)", [$respId, $q['id'], $survey['id'], $answerText, $answerVal, $answerJson] ); } } DB::query("UPDATE survey_responses SET status='complete', completed_at=NOW(), time_taken_seconds=? WHERE id=?", [max(1, (int)($_POST['_time_elapsed'] ?? 0)), $respId]); DB::query("UPDATE surveys SET response_count=response_count+1, complete_count=complete_count+1 WHERE id=?", [$survey['id']]); $user = DB::row("SELECT user_id FROM surveys WHERE id=?", [$survey['id']]); if ($user) DB::query("UPDATE users SET responses_used_this_month=responses_used_this_month+1 WHERE id=?", [$user['user_id']]); $redirectUrl = $settings['redirect_url'] ?? ''; if ($redirectUrl) { header("Location: $redirectUrl"); exit; } echo renderThankYou($survey, $settings, $theme); exit; } function renderClosed($survey) { return 'Survey Closed
πŸ”’

This survey is closed

Thank you for your interest. This survey is no longer accepting responses.

'; } function renderThankYou($survey, $settings, $theme) { $msg = htmlspecialchars($settings['thank_you_message'] ?? 'Thank you for completing this survey!'); $primary = $theme['primary_color'] ?? '#e94560'; return 'Thank You
βœ“

All done!

' . $msg . '

'; } ?> <?= htmlspecialchars($survey['title']) ?>
πŸ‘ PREVIEW MODE β€” Responses are not recorded
Logo

Page 1 of
$page): ?>
1): ?>

$q): renderQuestion($q, $qi); endforeach; ?>
0): ?>
Survey image
*' : ''; $o = $q['options'] ?? []; $s = $q['settings'] ?? []; ?>
>
$choice): $soUrl = htmlspecialchars($o['screenout_urls'][$ci] ?? ''); ?>
$choice): $soUrl = htmlspecialchars($o['screenout_urls'][$ci] ?? ''); ?>
>
placeholder="Enter a number…" > >
$stmt): ?>
$row): ?> $col): ?>
$item): ?>
β Ώ
Distribute exactly points. Remaining:
>
Max file size: MB