I have a search bar and a button to search on my page. I would like to know how to save user inputs, to have a historical about what the users try to find on the search page. I have 2 datas sets, one to show the results from the search (it works) and another one to save what the people are searching (doesn’t work).
import wixData from 'wix-data';
import {local} from 'wix-storage';
Define a variable as an array to save the search locally.
let searches = [];
Add en event handler on the search button to work whenever it is clicked.
$w('#searchButton').onClick((event) => {
// Define a variable for the search value
let search = $w('#searchInput').value;
// Check if the search is not empty before saving..
if (search !== '') {
// Check if the search is saved in the array.
let items = searches.filter(term => {
if (term === search) {
return term;
}
})
if (items.length > 0) {
// Ignore this search term (already saved locally)
} else {
// This search isn't saved locally, so save it.
// First, add it to the array.
searches.push(search);
// Then save it locally.
local.setItem('searches', JSON.stringify(searches));
}
/* Now save the search to the database, regardless
of if it's saved locally or no */
wixData.insert('collection', {searchBar: search})
}
})
@Arthur Valatin , if your searches are being saved only when users are logged in, you probably have your collection’s permissions set to ‘Member Generated Content’, which allows only members to submit data to the database, use custom permissions and set the “Write” permission to “Anyone”.
I used this code and in ‘collection’ I used the collection’s ID.
Where can I find ‘searches’? I have to do another database with this name?
Sorry for the question but there’s someone that I didn’t get.
@gabidvsala ‘searches’ is an array used to save the searches locally on the user’s browser cache, it’s like cookies.
Also, when inserting the search inside the collection, I did insert it in a database where all the searches are stored, so yeah, ‘collection’ is just a keyword I used to refer to the collection ID that you should use to store the searches.