Pre-select radio button on a form, based on the source page

Hi everyone,

I am trying to achieve on a Wix Form, that a given radio button is pre-selected for the user, if they arrive to the Form from a given page.
I am trying to use the session storage (not sure if memory would be better). Here is the code from the source page, where if certain buttons or text is clicked, it leads to the form, while setting the storage:
import { session } from ‘wix-storage’ ;

$w ( ‘#button1’ ). onClick ( function () {
session . setItem ( “formSource” , “service” );
});

$w ( ‘#button23’ ). onClick ( function () {
session . setItem ( “formSource” , “service” );
});

$w ( ‘#text72’ ). onClick ( function () {
session . setItem ( “formSource” , “service” );
});

And here is the code from the Form:
import { session } from ‘wix-storage’ ;
$w . onReady ( function () {
let sourcePage = memory . getItem ( “formSource” );
if ( sourcePage === “service”) { $w ( ‘#radioGroup2’ ). selectedIndex = 0 }
})
There would be some more ‘else if’ statements coming based on other source pages as well.

This doesn’t actually work. What am I doing wrong? Is there a better implementation maybe? When this code is active on the source page, one of the buttons doesn’t actually do anything either, which seems like a bug.

Thanks in advance for any tips and help!

You are doing almost everything wrong.

  1. Your buttons do not really work, maybe because you do not put them into → onReady()
import {session} from 'wix-storage';

$w.onReady( function() {
    $w('#button1').onClick(()=> {session.setItem("formSource", "service");});
    $w('#button23').onClick(()=>{session.setItem("formSource", "service");});
    $w('#text72').onClick(()=>  {session.setItem("formSource", "service");});
});


:thinking: hmmm, still something missing or wrong!
Why all buttons do same action ? —>

{session.setItem("formSource", "service");});

You are sure know how to use Wix-Storage?

Maybe you want to read one more time here…

Ok, lets say, you did everything the correct way and your wished data is inside STORAGE.

Lets jump over to the form… (oh look, here you are using onReady()!!!)

But why MEMORY now? Did’t you save the VALUE into SESSION ???

Saving into SESSION, but getting from MEMORY? Hmmm, never tried this before!

import {session} from 'wix-storage';

$w.onReady( function() {console.log("Page is ready / Form is ready);
    let sourcePage = memory.getItem("formSource");
    if (sourcePage === "service") {$w('#radioGroup2').selectedIndex = 0}
});

What if you try to get your SAVED-VALUE from → SESSION ?

import {session} from 'wix-storage';

$w.onReady( function() {
    let sourcePage = session.getItem("formSource");
    if (sourcePage === "service") {$w('#radioGroup2').selectedIndex = 0}
});

Even your IMPORT is → SESSION <— and not → MEMORY <— !!!

import {session} from 'wix-storage';

You should use more the CONSOLE!!! → This will help you a lot to understand your own CODE!

Thanks for your kind answer!
The memory and session mixup was already from me trying to solve the issue, which was also present when I actually tried to get the data from the correct storage :slight_smile: And it is also present now that I fixed the code with your suggestions.
Some buttons stop working when the code is implemented (not opening the linked page). The buttons that do actually keep working now select the correct radio!