connect_error) { die("Connection failed: " . $db->connect_error); } $survey_id = $_GET['id'] ?? 0; $page = $_GET['page'] ?? 1; // Find the unique_id and appended value $unique_id = ''; $appended_value = ''; foreach ($_GET as $key => $value) { if ($key !== 'id' && $key !== 'page') { $unique_id = $key; $appended_value = $value; break; } } if (!$unique_id || !$appended_value) { die("Invalid survey link. Missing unique ID or appended value."); } // Debugging output echo "Debug Info:
"; echo "Survey ID: $survey_id
"; echo "Unique ID: $unique_id
"; echo "Appended Value: $appended_value
"; // Check if the survey has already been completed with this appended value $stmt = $db->prepare("SELECT * FROM responses WHERE survey_id = ? AND appended_value = ? LIMIT 1"); if (!$stmt) { die("Prepare failed: " . $db->error); } $stmt->bind_param("is", $survey_id, $appended_value); if (!$stmt->execute()) { die("Execute failed: " . $stmt->error); } $result = $stmt->get_result(); $existing_response = $result->fetch_assoc(); $stmt->close(); echo "Existing Response: " . ($existing_response ? "Yes" : "No") . "
"; if ($existing_response) { echo "Response Details:
" . print_r($existing_response, true) . "

"; } if ($existing_response) { $status_message = "You have already completed the Survey with the unique value '$appended_value' with status \"{$existing_response['status']}\". Please try entering the survey with a different survey value."; include 'error_page.php'; exit; } // Fetch survey details $survey = $db->query("SELECT * FROM surveys WHERE id = $survey_id")->fetch_assoc(); if (!$survey) { die("Survey not found."); } // Fetch current page details $current_page = $db->query("SELECT * FROM pages WHERE survey_id = $survey_id AND page_number = $page")->fetch_assoc(); // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && $page >= 2 && $page <= 4) { $choice_id = $_POST['choice'] ?? null; if ($choice_id) { $result = $db->query("SELECT redirect_url FROM choices WHERE id = $choice_id"); $redirect = $result->fetch_assoc()['redirect_url']; if ($redirect) { $status = ''; switch ($page) { case 2: $status = 'Terminate'; break; case 3: $status = 'Overquota'; break; case 4: $status = 'QualityTerminate'; break; } // Record the response only if there's a redirect $stmt = $db->prepare("INSERT INTO responses (survey_id, unique_id, appended_value, status) VALUES (?, ?, ?, ?)"); $stmt->bind_param("isss", $survey_id, $unique_id, $appended_value, $status); $stmt->execute(); $stmt->close(); // Modify the redirect URL to replace the placeholder with the appended value $parsed_url = parse_url($redirect); if (isset($parsed_url['query'])) { parse_str($parsed_url['query'], $query_params); foreach ($query_params as $key => $value) { if (empty($value)) { $query_params[$key] = $appended_value; break; } } $parsed_url['query'] = http_build_query($query_params); } $redirect = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'] . '?' . $parsed_url['query']; header("Location: $redirect"); exit; } } $page++; header("Location: ?id=$survey_id&page=$page&$unique_id=$appended_value"); exit; } // Handle completion page submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && $page == 5) { $stmt = $db->prepare("INSERT INTO responses (survey_id, unique_id, appended_value, status) VALUES (?, ?, ?, 'Complete')"); $stmt->bind_param("iss", $survey_id, $unique_id, $appended_value); $stmt->execute(); $stmt->close(); $result = $db->query("SELECT redirect_url FROM choices WHERE page_id = {$current_page['id']} LIMIT 1"); $redirect = $result->fetch_assoc()['redirect_url']; if ($redirect) { // Modify the redirect URL as before $parsed_url = parse_url($redirect); if (isset($parsed_url['query'])) { parse_str($parsed_url['query'], $query_params); foreach ($query_params as $key => $value) { if (empty($value)) { $query_params[$key] = $appended_value; break; } } $parsed_url['query'] = http_build_query($query_params); } $redirect = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'] . '?' . $parsed_url['query']; header("Location: $redirect"); exit; } } ?> <?= htmlspecialchars($survey['title']) ?>

Start Survey = 2 && $page <= 4 && $current_page): ?>

Page

query("SELECT * FROM choices WHERE page_id = {$current_page['id']}"); while ($choice = $choices->fetch_assoc()): ?>

Page 5 (Completion)

Survey Completed or Page Not Found

Thank you for your participation.