Postal code check in the database

I’m currently attempting to allow users to enter their postal code into a form and once they click, the results should differ depending on their postal code. However, even when the postal code exists in the database, it doesn’t pick it up for some reason. Please assist, thank you. This is my current code:


import wixUsers from ‘wix-users’;
import {local} from ‘wix-storage’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
import wixWindow from ‘wix-window’;

$w.onReady(function () {
$w(‘#button1’).onClick((event, $w) => {
onClick(event, $w);
})
})
export function onClick(event, $w){
$w(‘#button1’).onClick(() => {
let postcode = $w(‘#input2’).value;
//Blank field exception
if(postcode === “”){
$w(‘#error’).show();
return;
}
wixData.query(“Postal_Code”).eq(“postal_code_ori”,postcode).find().then((results) =>{
let resultCount = results.totalCount;
//Check that user with the postal code exists
if(resultCount === 0){
wixWindow.openLightbox(“Subscribe (Black & White)”);
}
if(resultCount > 0){
wixLocation.to(‘/shop’);
}
}, (err) => {
console.log(err);
})
})
}

If you remove the first line of the onClick function code, and its accompanying brace and parenthesis down below, it should find a postal code that is in the collection. You had already defined the button1 onClick in the onReady.

Hi @tony-brunsman , thanks for the response. Just be clear, are you suggesting I remove “export function onClick(event, $w){”? And when you say “brace and parenthesis” are you referring to the brackets?

Sorry, I should have been more clear. It’s the line below it:

$w(‘#button1’).onClick(() => {

And the last pair of “})”

Hi, I have online store on wix. how i can add this coding. pleasae help!

Could someone please post the entire code?

Hi, I’ve implemented this to my site this morning. The above code (without the part Anthonyb suggested) works and is the entire code. The main thing you need to do is ensure your item names match up to the code. The names for the following:
• Postcode database
• Postcode field within the database
• Form text entry box (where the user enters the postcode)
• Form click box (below the text entry box)
• The lightbox for if it doesn’t find a postcode (named Subscribe (Black & White) in the code)
• The code sends the user to the /shop page if it finds a postcode match. If you don’t have a shop page, you can change this code to the same as the lightbox, with a different name, say ‘Delivery allowed’ with a message saying you deliver to their address.

Hope that helps.

When youn want delet the 2x click function remove this:

$w.onReady(function () {
	$w('#button1').onClick((event, $w) => {
		onClick(event, $w);
	})
})

And change to this:

$w.onReady(function () {
$w('#button1').onClick((event, $w) => {
		let postcode = $w('#input2').value;
		//Blank field exception
		if(postcode === ""){
			$w('#error').show();
			return;
		}
		wixData.query("Postal_Code").eq("postal_code_ori",postcode).find().then((results) =>{
			let resultCount = results.totalCount;
			//Check that user with the postal code exists
			if(resultCount === 0){
				wixWindow.openLightbox("Subscribe (Black & White)");
			}
			if(resultCount > 0){
 wixLocation.to('/shop');
			}
		}, (err) => {
			console.log(err);
		})
	})
}

Can u help me plz in this i am new :no_mouth:

Can you please post the full code