Send user to different page based on data input

Hello! I’m trying to create a really simple thing (I think).
We want users to input a zipcode, and depending on the answer, send them to a “we’re not in your state” page or a page with a signup form to get more data. I have all the “correct” zipcodes in a collection. It doesn’t need to look at live zipcodes or pull from Google.

This is the page: https://www.surroundins.com/home-test
This is the code I thought would work, but it’s not:

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

export function searchzip_click(event) {
 let SearchValue = $w("#searchbar").value
  $w("#dataset1").setFilter(wixData.filter().contains('zipCodes', SearchValue))
  .then(() => {
 let count = $w("#dataset1").getTotalCount();
 if (count === 0) {
         wixLocation.to("/out-of-range");
      }
 else {
         wixLocation.to("/get-a-quote");
      }}
  )}

Help is greatly appreciated!

First of all, some country only have numerical zip codes while others allow diferent characters as well. So if you set the the zip code field in your database to be Type:number you should change the input to type number as well:

Number(SearchValue)

If the field is of Type:text then ignore this comment.

Second, try to .refresh() the dateset after you set the filter.

Third, maybe the performance will be better if instead of filtering the dataset, you’ll get all items and then use JS .filter() method to filter the items array (you;ll have to test whether or not it makes the performance better).

The type is text. It’s just a field with all the possible zipcodes in it.

Imagine it’s not zipcodes. Not this big number thingy.

Search: apple, banana, orange, grapes (words in the dataset)
Returns page 01

Search: anything else
Returns page 02

Check if the _click function is turned on (on the property panel).

Ok, that seemed to do it! When I clicked onClick it made new code, and I cut and pasted that where the old code was. It added an “_1.” Not going to question it

Also, I’m feeling pretty proud that it was a dumb answer! That means I’m getting better at writing custom code!

:slight_smile:

I checked the website and the button seems to work just fine, also I think that your code will work, and it doesn’t need any changes, did you already solve your issue?

Ok, that seemed to do it! When I clicked onClick it made new code, and I cut and pasted that where the old code was. It added an “_1.”

It will do as you already had that onClick event function name used in your existing code on your page already.

export function searchzip_click(event) {

If you already have the same event function name in your used code it will automatically add an extra ‘_1’ to the end of the event function name so that you can distinguish between the two.

Yup! It works now, see the above solution.