RESOLVED - Find table data and make redirect

Goodmorning everyone,
I started using the new wix code feature and it’s great, but I have a problem I do not know how to solve it.

I created a table with a column of type text (heading CODE) and a coloanna of type url (called redirect).

In the public side or enter a text input field and a button How to proceed to search the data entered in my column “CODE” once pressed the send key, and load the page corresponding to the URL.

Thanks for your help Daniel

Maybe you are making life a bit too difficult. If you want users to fill out something, and then have the result shown on a second page, you need to remember what the search criteria were, because of the stateless design of the Internet.
“Remembering” can be done, usually, in 2 ways: you pass parameters to a second page (newpage?Crit=foo) or you store it on client side disk and on the second page read it back. The last one is the Wix way.

To make it less difficult, couldn´t you display everything on the same page (search and display) and get rid of the URL?

Thanks for the answer, I think I explained myself badly.

I attach the image to explain better

When in the public side I enter the code and then I press the button, I have to send the user in the page corresponding to the url of the code inserted in the table

I hope to be explained correctly

Ah, now I get it, thank you. OK, you would need to do the following:

  1. get rid of any dataset on your page, we are going to do it all in code
  2. import wix-data and perform a query with the input from the textbox as parameter https://www.wix.com/code/reference/wix-data.html#query
  3. use the .totalcount property to establsih if you have at least 1 result (>0) https://www.wix.com/code/reference/wix-data.WixDataQueryResult.html#totalCount
  4. if you have at least 1 result, get the currentitem to find the URL and use wixlocation.to() to navigate to the page in URL (See wix-location-frontend - Velo API Reference - Wix.com )(you will have to do a:

import wixLocation from ‘wix-location’;

at the top of your code)

instead of currentitem, use items

Thank you for your answer, very kind.

Unfortunately it is too difficult, also because in the guides there are no specific aspects or examples of how to make the code even for those who are not as expert as me.

I write you what or wrote, but I have many gaps regarding:

  • Where I apply the code, to the button or to the input box
  • How can I insert the result of the text box in the query
  • How can I find the URL corresponding to the code?
  • How to connect the various instructions together

As you can see there are many doubts, and I do not know if you want to help me. I think once you see how you proceed the first time then with a bit of logic and some manual can understand how to do, but for the moment it is difficult for me. Anyway I’ll write you the code I tried to do for your help if you want to help me

// … What does this data refer to? do i have to change it?

import wixData from ‘wix-data’;

// … How can I import data by entering the text box?

let query = wixData.query (“RedirectCode”);

// … How do you know what to look for to give me the result?

wixData.query ( “RedirectCode”)
.find ()
.then ((results) => {
let resultCount = results.totalCount; // 150
});

// … This data what does it charge? is it for?

import wixLocation from ‘wix-location’;

// … How do you know where to go, where?

wixLocation.to ( “http://wix.com”);

As you have seen I know very little about this code, I hope you can help me get the first notions about it

Thank you

I understand how you must feel. When I started with Wix Code half a year ago, I uttely hated it, it felt like I was swimming in deep water without a life jacket. Did not know much about Javascript, did not understand the model (pulling data in from client, instead of pushing html towards client), the database was an enigma, documentation was still sparse and the asynchronous paradigm, with Promises, oh, I loathed it.
But don´t give up. There is one way and only one way to master it: try. Read the Examples, try them out, study the API, read w3schools about Javascript, look for answers on Stackoverflow or on the Forum before you ask one and, above all, console.log is your friend. And then, if you have really tried out everything and can´t work it out, you come here and ask for help. Which I will try to give you now:

// … What does this data refer to? do i have to change it?
import wixData from ‘wix-data’;
Wix has seperated there logic into discrete parts. If you need to do anything with ‘users’ (login, passwords) you declare that you want to use that part of Wix in your code. That is called ‘import’. If you want to do anything with the database, you use, or import ‘wix-data’. Why do you have to do that? Why is not everything readily available? Answer: resources. If Wix has to load all this logic for every page and you only use 1 part of it. 90% of time and speed is waisted.

/ … How can I import data by entering the text box?
let query = wixData.query (“RedirectCode”);

Here, you are going to study. But I will give you a clue. Wix is event driven, like most develpment environments. That means nothing happens until a user or a function raises an event. In the case of a user, when he clicks something. SO you put in a text box, you add a button and when the button is clicked (event), you read the text (value) from the text box and do something with it. Good luck, there are enough examples of this.

// … How do you know what to look for to give me the result?
wixData.query ( “RedirectCode”)
.find ()
.then ((results) => {
let resultCount = results.totalCount; // 150
});

The Wix database (and, currently, any other db) is based on a query/queryresult paradigm. This means that you have rows in your collection, but when you want to do something with that data, you use a query, a question. Like: give me all rows where user = “AAA”, or where “date is greater then today”. Then, the database executes this query and returns a result, called a Resultset or QueryResult. Physically, this is a copy of a part of the db, with all rows that correpond to a query. So you act upon that copy, you never, ever work with the ‘real’ row in the database. You can only, thru the interface, add, update or delete .
So now you are going to read the API documentation on wix-data, the query object and the query result object and try out every example that you can find, until it becomes clear.

// … This data what does it charge? is it for?
import wixLocation from ‘wix-location’;
// … How do you know where to go, where?
wixLocation.to ( " http://wix.com ");

See ‘wix-data’, above. This is another discrete part of logic. So if you want to jump from location to location (URL), you load, import, that logic and work with it. BTW, all imports should be at the top of your code, one underneath the other, NEVER in a function.

Good luck, don´t give up, in a couple of months you will smile about the questions that you asked now.

2 Likes

Hello,

nothing to do, 2 days I try to understand how to do, but having no code written correctly I can not understand.

Eg:

import wixData from ‘wix-data’;

export function buttonlogcode_click (event) {
wixData.query (‘RedirectCode’)
.eq (‘Code’, ‘imputcode’)
.find ()
.then ((results) => {
let item = results;
let resultCount = results.totalCount;
})
.catch ((err) => {
let errorMsg = err;
});
}

I also attach scrin shot, because where appears the yellow traingolo gives me a unread. How can I see the result once the button is pressed?

You can help me with this first code by writing it in the correct way so as to understand.

Thank you

Hi,
If anyone needs the code I’ve arrived at, I’ll write it down below. I do not know if it’s the best way, but it’s the one I found.

export function buttonlogcode_click_1() {
wixData.query(‘RedirectCerimonie’)
.eq(‘code’, $w(‘#imputcode’).value)
.find()
.then(results => {
let resultCount = results.totalCount;
if (resultCount !== 0) {
let items = results.items;
let item = items[0];
let utenteCode = item.utenteRedirect;
let cartella = item.db_cartella;
let nomeEvento = item.nomeEventoOErimonia;
console.log(‘utenteCode + ‘+ utenteCode);
if (utenteCode === ‘Sposi’) {
wixLocation.to(‘http://www.danielphotographic.it/gallery/’ + cartella + ‘/index.php’);
} else {
wixLocation.to(‘http://www.danielphoto.it/’ + nomeEvento.toLowerCase());
}
} else {
$w(’#errorLine’).text = ‘Il Codice Evento inserito non è valido.’;
$w(‘#errorLine’).show();
}
});
}