diff --git a/assets/js/main.js b/assets/js/main.js
index e2f8e2e..015b706 100644
--- a/assets/js/main.js
+++ b/assets/js/main.js
@@ -55,26 +55,40 @@ document.addEventListener('DOMContentLoaded', function () {
spinner.classList.remove('d-none');
submitButton.disabled = true;
- // --- FAKE LOADER FOR DEMO ---
- // In the next step, we will replace this with a real AJAX call to the backend.
- setTimeout(() => {
+ const formData = new FormData(uploadForm);
+
+ // Show loading state
+ resultsSection.style.display = 'block';
+ analysisOutput.innerHTML = `
+
+
+ Loading...
+
+
AI is analyzing your data, this may take a moment...
+
+ `;
+ resultsSection.scrollIntoView({ behavior: 'smooth' });
+
+ fetch('upload.php', {
+ method: 'POST',
+ body: formData
+ })
+ .then(response => response.json())
+ .then(data => {
+ if (data.success) {
+ analysisOutput.innerHTML = data.analysis;
+ } else {
+ analysisOutput.innerHTML = `An error occurred while communicating with the server.
`;
+ })
+ .finally(() => {
// Hide spinner and re-enable button
spinner.classList.add('d-none');
submitButton.disabled = false;
-
- // Show results section with a placeholder message
- resultsSection.style.display = 'block';
- analysisOutput.innerHTML = `
-
-
- Loading...
-
-
AI is analyzing your data... This will be implemented in the next step.
-
- `;
- // Scroll to results
- resultsSection.scrollIntoView({ behavior: 'smooth' });
-
- }, 2000);
+ });
});
});
diff --git a/upload.php b/upload.php
new file mode 100644
index 0000000..3554623
--- /dev/null
+++ b/upload.php
@@ -0,0 +1,100 @@
+ false, 'error' => $message]);
+ exit;
+}
+
+// 1. Handle File Upload
+if (!isset($_FILES['csv_file']) || $_FILES['csv_file']['error'] !== UPLOAD_ERR_OK) {
+ return_error('File upload error. Please try again. Code: ' . ($_FILES['csv_file']['error'] ?? 'Unknown'));
+}
+
+$file = $_FILES['csv_file'];
+$tmpPath = $file['tmp_name'];
+
+// 2. Validate File Type (Basic Check)
+$fileType = mime_content_type($tmpPath);
+$fileName = $file['name'];
+$fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
+
+if ($fileType !== 'text/csv' && $fileType !== 'text/plain' && $fileExtension !== 'csv') {
+ return_error('Invalid file type. Please upload a valid CSV file.');
+}
+
+// 3. Read CSV Header and First 2 Rows
+$dataSample = '';
+$handle = fopen($tmpPath, 'r');
+if (!$handle) {
+ return_error('Failed to open the uploaded file.');
+}
+
+$lineCount = 0;
+while (($row = fgetcsv($handle)) !== false && $lineCount < 3) {
+ $dataSample .= implode(',', array_map(function($cell) {
+ return '"' . addslashes($cell) . '"';
+ }, $row)) . "\n";
+ $lineCount++;
+}
+fclose($handle);
+
+if (empty($dataSample)) {
+ return_error('The CSV file appears to be empty or could not be read.');
+}
+
+// 4. Prepare Prompt for AI
+$prompt = <<