I created a searchbar using javascript code that will help me search for a particular data from my database, but instead of bringing what’s related to my search, it brings out all the data in my database
This is the javascript code
document.querySelector('form').addEventListener('submit', function(event) {
event.preventDefault();
const searchTerm =
document.getElementById('searchInput').value;
fetch(`http://localhost:7007/search?q=${encodeURIComponent(searchTerm)}`)
.then(response => response.json())
.then(data => {
if(data['error'] !== true){
console.log(data)
console.log("No errors detected")
data.forEach(found => {
newNews(found);
});
}else{
alert(result['message'])
}
})
.catch(error => {console.error('Error fetching data:',error);
});
});
This is the input in the html
<form>
<div class="for">
<i id="ico" class="fa-solid fa-magnifying-glass"></i>
<input class="input" id="searchInput" type="text" placeholder="SEARCH ANY NEWS" style="width: 100%; border-left: none;">
<button type="submit">Search</button>
</div>
</form>