diff --git a/assets/js/main.js b/assets/js/main.js index bc96fdc..01b8c03 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -41,4 +41,79 @@ document.addEventListener('DOMContentLoaded', function () { ].join(''); alertContainer.append(wrapper); } + + // --- Search Functionality --- + const searchForm = document.getElementById('searchForm'); + if (searchForm) { + const searchInput = document.getElementById('searchInput'); + const searchButton = document.getElementById('searchButton'); + const searchResultsSection = document.getElementById('searchResults'); + const resultsContainer = document.getElementById('resultsContainer'); + const noResults = document.getElementById('noResults'); + + searchForm.addEventListener('submit', function(e) { + e.preventDefault(); + const query = searchInput.value.trim(); + + if (query === '') { + return; + } + + // Show loading state + searchButton.disabled = true; + searchButton.innerHTML = ' Searching...'; + resultsContainer.innerHTML = ''; + noResults.style.display = 'none'; + searchResultsSection.style.display = 'block'; + + fetch(`search.php?q=${encodeURIComponent(query)}`) + .then(response => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return response.json(); + }) + .then(data => { + if (data.length > 0) { + displayResults(data); + } else { + noResults.style.display = 'block'; + } + }) + .catch(error => { + console.error('Search error:', error); + resultsContainer.innerHTML = '
An error occurred while searching. Please try again.
'; + }) + .finally(() => { + // Restore button state + searchButton.disabled = false; + searchButton.innerHTML = 'Search'; + }); + }); + + function displayResults(products) { + products.forEach(product => { + const pricesHtml = product.prices.map(p => ` +Your smart shopping companion for comparing prices across South African retailers.
- - Download on Google Play - +