34796-vm/submit.php
2025-10-08 11:52:31 +00:00

46 lines
1.6 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
header('Content-Type: application/json');
$response = ['success' => false, 'message' => 'An unexpected error occurred.'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$satisfaction = filter_input(INPUT_POST, 'satisfaction', FILTER_SANITIZE_STRING);
$comments = filter_input(INPUT_POST, 'comments', FILTER_SANITIZE_STRING);
$how_heard = filter_input(INPUT_POST, 'how_heard', FILTER_SANITIZE_STRING);
if (empty($satisfaction)) {
$response['message'] = 'Satisfaction level is a required field.';
echo json_encode($response);
exit;
}
try {
$pdo = db();
$sql = "INSERT INTO survey_responses (satisfaction, comments, how_heard) VALUES (:satisfaction, :comments, :how_heard)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':satisfaction', $satisfaction);
$stmt->bindParam(':comments', $comments);
$stmt->bindParam(':how_heard', $how_heard);
if ($stmt->execute()) {
$response['success'] = true;
$response['message'] = 'Thank you! Your feedback has been submitted successfully.';
} else {
$response['message'] = 'Failed to save your response. Please try again.';
}
} catch (PDOException $e) {
// In a real app, you'd log this error. For now, send a generic message.
// error_log('PDOException: ' . $e->getMessage());
$response['message'] = 'Database error: Could not save your response.';
}
} else {
$response['message'] = 'Invalid request method.';
}
echo json_encode($response);