Comparing 2 inputs with data set.

Hi, can you guys help me with something ?
I’ve created page that has input fields of Name and Phone Number. I’ve also created a data collection “Client” that has the fields Name and Phone Number. what I’m trying to do is after input of correct Name and Phone Number to go to the item dynamic page. I don’t need this to work like login. I do not need to verify the Client. I just need to go to item dynamic page if 2 inputs are the same as in data set. How can I make it work. Can someone give me the code to make this happen?

Hi Arman!

Basically you got 2 input fields that you want to check as if they appear in your key database.
What you’ll want to do is to check those inputs value on the occasion of change - onChange( ) .
Once you get a valid value\input you want to redirect your page to the relevant page using the
wixLocation API .
Its gonna look something like that (giving you use a submit button):

import wixData from 'wix-data';
import wixLocation from 'wix-location';

export function yourInput_click(event, $w) {
	let input1 = $w('#input1').value;
	let input2 = $w('#input2').value;
	wixData.query("yourDatabase")
		.eq('dataBaseFieldName1', input1)
		.eq('dataBaseFieldName2', input2)
		.find()
		.then((results) => {
			if (results.items.length > 0) {
				wixLocation.to(`/${results[0]._id}`);
				//or your desired location.
			}
		});
}

Hope it helps!
Best of luck!
Doron. :slight_smile:

Dear Doron,
Thanks for your Help. I have done all the steps correctly but it is not working. Here is the screenshot attached.

Hi Arman!

First of all:
***** Please refrain from posting your personal user info! Other forum users can see your posts and comments! *****

Now,
After looking into your site (which looks great!) and into the code of the page mentioned above,
I’ve noticed a few corrections to be made. Your code should look like this:
Please make sure to read my comments :slight_smile:

import wixData from 'wix-data';
import wixLocation from 'wix-location';

export function button12_click(event, $w) {
	let username = $w('#username').value;
	let password = $w('#password').value;
	wixData.query("AllStudents")
		.eq('nameSurname', username)
		.eq('phone', password)
		//make sure the field names that you search in the query
		//are the same as the actual fields in the data base.
		.find()
		.then((results) => {
			if (results.items.length > 0) {
				wixLocation.to(`/admin-student/${results.items[0]._id}`);
				// using the wix location API,
				// make sure you return the full destination URL 
			}
		})
		.catch((err) => {
			console.log("err", err);
		});

}

It should solve your problem, but, if you got further questions regarding this issue I’d be glad to help you!

Doron. :slight_smile:

Dear Doron,
Thanks For Your Support. Now it is working. I have one more question and that’s it. Please can you tell me how can I add a ‘User not found’ text if the entered values didn’t match with the data set?

Hi,
You can create a text element and set it to hidden onload in properties panel.
Then,

 if (results.items.length === 0) {
   $w('#yourTextElement').show();
 }

Good luck!
Roi.

import wixData from 'wix-data';
import wixLocation from 'wix-location';

export function vectorImage1_click(event, $w) {
	let username = $w('#username').value;
	let password = $w('#password').value;
	wixData.query("AllStudents")
		.eq('month', username)
		.eq('paid', password)
		.find()
		.then((results) => {
			if (results.items.length > 0) {
				wixLocation.to(`/account/${results.items[0]._id}`);
			
			if (results.items.length === 0) {
   $w('#text113').show();
 }
				
			}
		})
	
		.catch((err) => {
			console.log("err", err);
		});

}

Here is my code. Everything is working expect the if (results.items.length === 0) { $w(‘#text113’).show(); } which I need to show when the user is not found.

Hi Arman!
It seems like you’ve put the second ‘if’ statement inside the first one without closing it first.
It should be like this:

if (results.items.length > 0) {
   wixLocation.to(`/account/${results.items[0]._id}`);
}
if (results.items.length === 0) {
   $w('#text113').show();
 }

Doron.

Dear Doron,
Thank You so much for your support. You helped me to create the thing I wanted. You are so kind. Thanks again…

Hey there,

Thank you for this post - it’s the closest to helping me solve a task on my website. I’m trying to include a feature on one of my pages, that compares a users input to a database, and redirects the user to a different page, or displays a text if a match is found.

Basically, they are inserting codes to see if they are a winner. I plan to upload the winning codes to the Wix database, then using some conditions to test for a match. Here’s what I have so far, but it can’t quite figure out the rest. Any advice?

import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

export function button1_click(event, $w) {
let code = $w(‘#input1’).value;
wixData.query(“winCodes”)
.eq(‘winCodes’, input1)
//make sure the field names that you search in the query
//are the same as the actual fields in the data base.
.find()
.then((results) => {
if (results.items.length > 0) {
wixLocation.to(/ENTER_CODE/${results.items[0]._id});
}
if (results.items.length === 0) {
$w(‘#text15’).show();
}
})

    . **catch** ((err) => { 
        console.log("err", err); 
    }); 

}

Hi Wix Team
I tried to apply the above approach on my on website’s code by it does not seem to be working for me can you please let me know what I am doing wrong below?

import wixData from ‘wix-data’ ;
import wixLocation from ‘wix-location’ ;

export function loginButton_click(event,$w) {

let username = $w( “#username” ).value;
let Password = $w( “#Password” ).value;

wixData.query( “CompanyProfile” )
.eq( “inputEmail” , username)
.eq( “inputPassword” , Password)
.find()
.then((results) => {
if (results.items.length > 0 ) {
wixLocation.to(/about1/{Company Name});
}

if (results.items.length === 0 ) {
$w( ‘#errorMsg’ ).show();
}
})

. catch ((err) => {
console.log( “err” , err);
});

}