Is it at all possible using Wix Code to set that (with being connected to a button) that when a user enters a specific text in a text box that when they push for example a “Next” button that it will only let them proceed if the text they entered is exactly the same as something already in a database?
I’m afraid I don’t know how to code, but this could be very useful in multiple applications of use.
It is possible, but you need to code for it.
I’ll share a small example.
Let’s say I have a database named ‘myPets’ of pet type and their names:
petType petName
Dog Bob
Cat Mitzi
And I have a textbox (‘#textInput1’) and a button (‘button1’) that tries to verify the name of the dog. The button should have an onClick() function that uses wixData.query() to check the database:
export function button1_click(event) {
const userInputText = $w('#textInput1').value;
wixData.query('myPets').eq('petType', 'Dog').find().then(result => {
const dogItem = result.items[0];
if (dogItem.petName === userInputText) {
//Validation success - do something
} else {
//Validation failure - do something else
}
})
}
Let me explain (and simplify a bit) the code Liran wrote -
export async function button1_click(event) {
// here we read the value from the input named textInput1
const userInputText = $w('#textInput1').value;
// here we query the database, to the collection myPets, with a condition that the field PetType equals Dog
let result = await wixData.query('myPets').eq('petType', 'Dog').find();
// the result has an array of items. we get the first item from the array
const dogItem = result.items[0];
// now we check if the petName from the database equals the petName entered by the user.
if (dogItem.petName === userInputText) {
//Validation success - do something
}
else {
//Validation failure - do something else }
}
}
On the second part - validation success and validation failure, you can navigate to another page using wixLocation or show a message using the show / hide APIs.
Try the following - have in the first line of the button click function the navigation code. Lets see if it works.
export async function button1_click(event) {
wixLocation.to("https://navitocompany.wixsite.com/private");
}
If it does, it means there is a problem with the function logic itself. The easiest way to understand that is to add console.log(‘some line comment’) calls to print stuff to the console, showing us what paths the code is running.
for instance, before the if line you can add a log of the parameters
console.log('before if', dogItem.petName, userInputText);
if (dogItem.petName === userInputText) {
Doing so will print, in the console, something like