Global variables?

What is the best way to use variables across multiple pages? Im not having luck using Wix storage. I am looking to make a website that asks 1 question per page and then takes the answers to those questions to determine a specific product. I just don’t know where I should define my variables and how to call them again from a different page.

@joeycahill24 Wix Storage is indeed the solution.

That being said, for multiple step form I would rather use a multi-StateBox with each state being one step of the form. That way you keep you context live and there is no need for wix Storage.

It has the added benefice to improve performances because only one page is loaded

Kentins idea seems to be the right way.
Sounds like a quiz, right?

If so…than you could take this example for your project, perhaps it can help a little bit…

https://russian-dima.wixsite.com/meinewebsite/little-quiz

But if you still want to use variables across multiple pages, then you have minimum 2-options, how to do it.

  1. The wixStorage-way
  2. The DATABASE-way

You said, that you have had no luck with Wix-Storage, but normaly it is a simple task.

You have site-A
You have site-B
You have a VALUE, which you want to transfer from site-A —> to site-B.

On site-A you put your wished → VALUE into the Wix-Storage, giving it a KEY-NAME…

local.setItem("myKeynameHere","value");

Now to get that stored VALUE back (out of the Wix-Storage), you will have to use…

local.getItem("myKeynameHere");

To do it in more detail…
SITE-A (putting a string → “Hello-World” into the Wix-Storage calling it → STRING)

import {local} from 'wix-storage';

$w.onReady(()=>{
	let myValue = "Hello-World"
	local.setItem("STRING", myValue);
});

SITE-B: Getting back the VALUE out of the Wix-Storage and putting it into a variable on Site-B…

import {local} from 'wix-storage';

$w.onReady(()=>{
   let myValue = local.getItem("STRING"); console.log("My-transfered-STRING: ", myValue)
});

Now, navigate to the CONSOLE of your Browser and take a look onto the results on site-B. If you are using → google-Chrome, than you can enter the console by pressing F-12 and navigating to —> CONSOLE. You also can use the console in the PREVIEW-MODE of your Wix-Editor.

Thank you! I looked into multi-stateboxs and that would work perfect for what I’m trying to do, unfortunately it is not available on Editor X (I should have specified).

So I am trying to use storage again but I have no idea what I’m doing wrong. I keep receiving “null” with no other indication of what went wrong.

Here is my first page:

And here is my second page that keeps returning “Null”

I really appreciate your help! Thank you again.

Yes! That’s exactly what I am looking to do. I appreciate your help!

I keep getting “Null” values when trying to use wix storage.

My first page of code is just store a users selected choice:

and page 2 is just reading that value: (Keeps returning Null)

Thank you again for all your help!

@joeycahill24

Doing this way, you also get → NULL/UNDIFINED as → RESULT ?

import {seesion, local} from 'wix-storage';

$w.onReady(()=>{
	let mySession = session.getItem("Sel1"); console.log("SESSION: ", mySession)
	let myLocal = local.getItem("Sel2"); console.log("LOCAL: ", myLocal)
});
1 Like

Thank you!!! This returned the proper value for Local, but session still returned null… I may just stick with local storage for now?

@joeycahill24
Yes, you can work just with local, why not?