Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Hash cookies & sessionStorage

Questions related to using nuBuilder Forte.
Post Reply
klasyc
Posts: 5
Joined: Thu Nov 11, 2021 4:25 pm

Hash cookies & sessionStorage

Unread post by klasyc »

Hello,

have a general question about hash cookies and session storage. I was able to find some information on this forum and in wiki, but I still cannot fully understand how they exactly work. Please could you check/correct my conclusions below?

1. For each item in a form, a hash cookie is automatically created after I POST the form. I.e. the hash cookie is created on PHP side and it also means I cannot reference the hash cookie from Javascript (I have to use jQuery to get field values). Then the hash cookie can be used in PHP custom code and in the JavaScript (nuGetProperty() function) code after the form is returned. Then the hashcookie disappears.

2. I can create a custom hash cookie by calling JavaScript function nuSetProperty(). It behaves the same way as the "automatic" hash cookie (it is valid for PHP code and returned form only) except I set the third parametr true. In this case the hash cookie has a global validity, but it's still limited to the scope of current browser window/iframe because the hash cookies are preserved as COOKIES in the browser window, right?

3. The session storage uses PHP $_SESSION array as a variable storage, therefore it is shared among all browser windows and has global validity.

4. When I call sessionStorage.setItem() from Javascript, the session variable is preserved in the browser until the form is sent and then it is stored in $_SESSION. In the other words, nuBuilder does not make asynchronous request to set the sessionStorage variable immediately when I call sessionStorage.setItem(). This implies when I use sessionStorage in onClick() event of Run object which opens a new tab, the new tab will not get fresh sessionStorage content yet because the original form has not been sent yet.

5. Session storage data cannot be used in SQL statements like #hash_cookies#.

Currently I am trying to make a form which contains a Run object. This run object opens another form which is restricted to a particular primary key. The primary key is provided by the form where the Run object resides. I want to filter the form according to the provided primary key and also when I create a new item in the form, I want to prefill my primary key.

My problem is that I need a hash cookie for the browse SQL statement, but also the session storage variable for the Edit form. I would prefer using one or another concept, but I don't like mixing of them :(

Thank you for your feedback!
Klasyc
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Hash cookies & sessionStorage

Unread post by kev1n »

HI,

In the form's BE event, use nuAddJavascript() to sets the current Form's property/to make it available for the form.

Code: Select all

nuAddJavascript('nuSetProperty("#YOUR_GLOBAL_HASH_COOKIE#")');
klasyc
Posts: 5
Joined: Thu Nov 11, 2021 4:25 pm

Re: Hash cookies & sessionStorage

Unread post by klasyc »

Hi Kevin,

thank you for your suggestion, this is very useful tip! Anyway, what about my questions about hash cookies and session storage? I really appreciate you post quick solutions here, but as a developer I would prefer to understand how the system exactly works because then I would be able to find the solutions myself. Also it would be very useful to have such information on wiki, but I don't know how to participate there.
kev1n
nuBuilder Team
Posts: 4297
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Hash cookies & sessionStorage

Unread post by kev1n »

Some additions and corrections:
I have to use jQuery to get field values
Use nuGetValue(), object_id.value or $('#object_id').val() to get a field's value.
can be used in PHP custom code
and in SQL (Display, Select objects)
and in the JavaScript (nuGetProperty() function) code after the form is returned.
Only hash cookies that are set with nuSetProperty can be retrieved with nuGetProperty(). But not the once that are automatically created.

but it's still limited to the scope of current browser window/iframe
No, global hash cookies exist in all browser windows/tabs/iframes.
because the hash cookies are preserved as COOKIES in the browser window, right?
They are not stored in cookies but in the session database table.

then it is stored in $_SESSION.
Session storage items are not stored in $_session. The information is stored in the user's browsing session, which is discarded by the browser when it is closed,
Post Reply