NetTalk Central

Author Topic: How to execute code once on Form Load?  (Read 209 times)

JohanR

  • Sr. Member
  • ****
  • Posts: 432
    • View Profile
    • Email
How to execute code once on Form Load?
« on: September 24, 2026, 02:43:47 AM »
Hi,

What is the preferred method or embed to ensure code only runs once on form generation, popup or page loaded.

I am currently using
if (p_web.GetValue('SignInResetPassword_form:inited_') = 1)

Using code below, it runs through twice, even if checking on p_web.SetValue('SignInResetPassword_form:inited_',1)
Attached code and debuglog for initform and preupdate code/logs


thanks for any advice

Johan


InitForm       Routine
  DATA
LF  &FILE
! Start of "Initalize the Form before anything else is done"
! [Priority 5000]
! End of "Initalize the Form before anything else is done"
  CODE
  ! Start of "Initalize the Form before anything else is done"
  ! [Priority 5000]

  ds_OutputDebugString('>>> InitForm <<<' & format(clock(),@t06))
  if (p_web.GetValue('SignInResetPassword_form:inited_') = 1)
     ds_OutputDebugString('========== 2nd time' & format(clock(),@t06))
  else
     ds_OutputDebugString('========== 1st time' & format(clock(),@t06))
  end
 
  ! End of "Initalize the Form before anything else is done"
  p_web.SetValue('SignInResetPassword_form:inited_',1)
  p_web.formsettings.file = ''
  p_web.formsettings.key = ''
  do RestoreMem
  ! Start of "Initalize the Form before anything else is done"
  ! [Priority 5000]
« Last Edit: September 25, 2026, 10:56:43 AM by JohanR »

rjolda

  • Sr. Member
  • ****
  • Posts: 471
    • View Profile
    • Email
Re: How to execute code once of Form Load?
« Reply #1 on: September 24, 2026, 12:48:58 PM »
Hi Johan,
I wanted to capture the last SAVE to File event and run code there.  I put an embed there and traced the stage each time that it ran and I found that p_stage=65536  was the embed point at the very end.  I put code there and it only runs ONCE.  So, I would suggest you place a debug trace on the embed you want and see what stage it runs at and see if you can isolate a stage (by number) that runs only ONCE.
I am not sure that it is best practice but it works ! ( forms can get called many times as the web server services other calls and keeps coming back to the open form for that user)
Ron

JohanR

  • Sr. Member
  • ****
  • Posts: 432
    • View Profile
    • Email
Re: How to execute code once of Form Load?
« Reply #2 on: September 25, 2026, 02:25:55 AM »
Hi Ron

Thanks for the info, I did have the stage as part of my debug log, but somehow I posted old screenshots.
The stage stays the same.

Something is triggering the form to be requested twice.
Between the devtools log and chatgpt, it's pointing at 2 requests
 

GET page
     ↓
generate sessionStorage.id
     ↓
GET /NewTabID
     ↓
set temporary x-TabID cookie
     ↓
location.reload()
     ↓
GET page again



One of the differences
SESSIONID=MrlXGbhPPdH9e4WrF5aRVl3dZUIvvp; x-TabID=4iunbrgg
and
Cookie: SESSIONID=MrlXGbhPPdH9e4WrF5aRVl3dZUIvvp

Still trying to understand why,

but the main goal is to somewhere in the init of the form have code that should only execute once.
So looking to check what to test and how to test.

In this case, a password reset link was emailed,
if the user clicks the link, the form is loaded twice and on the second instance it blocks as the link has been used already.

I can code around this somehow, but I still would like to understand why it executes twice,
as there could be other forms or situations with the same behavior.

As with these problems I never know if I caused an issue in my code and I end up chasing all sorts of avenues. :)

thanks

Johan


GET /SignInResetPassword?coded_rdis_isn=V2AN2KI6R0VHE92QX55O3R61002Z0Z446410TQZV00I2FZDR18 HTTP/1.1 Host: localhost:442 Connection: keep-alive Cache-Control: max-age=0 sec-ch-ua: "Google Chrome";v="153", "Not_A Brand";v="8", "Chromium";v="153" sec-ch-ua-mobile: ?0 sec-ch-ua-platform: "Windows" Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/153.0.0.0 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7 Sec-Fetch-Site: same-origin Sec-Fetch-Mode: navigate Sec-Fetch-Dest: document Referer: https://localhost:442/SignInResetPassword?coded_rdis_isn=V2AN2KI6R0VHE92QX55O3R61002Z0Z446410TQZV00I2FZDR18 Accept-Encoding: gzip, deflate, br, zstd Accept-Language: en-US,en;q=0.9 Cookie: SESSIONID=MrlXGbhPPdH9e4WrF5aRVl3dZUIvvp; x-TabID=4iunbrgg


GET /SignInResetPassword?coded_rdis_isn=V2AN2KI6R0VHE92QX55O3R61002Z0Z446410TQZV00I2FZDR18 HTTP/1.1 Host: localhost:442 Connection: keep-alive Pragma: no-cache Cache-Control: no-cache sec-ch-ua: "Google Chrome";v="153", "Not_A Brand";v="8", "Chromium";v="153" sec-ch-ua-mobile: ?0 sec-ch-ua-platform: "Windows" Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/153.0.0.0 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7 Sec-Fetch-Site: same-origin Sec-Fetch-Mode: navigate Sec-Fetch-User: ?1 Sec-Fetch-Dest: document Referer: https://localhost:442/SignInResetPassword?coded_rdis_isn=V2AN2KI6R0VHE92QX55O3R61002Z0Z446410TQZV00I2FZDR18 Accept-Encoding: gzip, deflate, br, zstd Accept-Language: en-US,en;q=0.9 Cookie: SESSIONID=MrlXGbhPPdH9e4WrF5aRVl3dZUIvvp



Johan

JohanR

  • Sr. Member
  • ****
  • Posts: 432
    • View Profile
    • Email
Re: How to execute code once of Form Load?
« Reply #3 on: September 25, 2026, 03:20:41 AM »

Hi,

Switching on Multi TAB support creates this, have tested it with one of the examples.

Is this correct?
How to test or which embed to use on first page load to run code only once for page setup?

I don't want to go down the road of trying to check for TAB id etc...if this process is not correct and there is something I am missing.




thanks

Johan

rjolda

  • Sr. Member
  • ****
  • Posts: 471
    • View Profile
    • Email
Re: How to execute code once of Form Load?
« Reply #4 on: September 25, 2026, 08:41:04 AM »
HI Johan,
This does NOT sound right.  Your procedure is actually getting called TWICE.  I would look into that as it should be called ONCE but it will loop through and set different stages.  Sounds like you have parallel procedures and they are clashing.  Seems like you need to see why that is happening.  I do the same type of password reset that you are talking about.  It is a Memory Web Form.  It has an ID that it grabs for password reset.
  loc:resetGUID = p_web.GetValue('id')   ! if comes in with a GUID for PW RESET
     
        !    message('Reset id: '   & loc:resetGUID)
  if loc:resetGUID <> ''
        p_web.SSV('loc:resetGUID', loc:resetGUID)  ! keep until done.
        p_web.SSV('tab',1)
  end     

I use another Memory Form to have them enter their E mail for password reset.  I look up the e-mail and if it exists, I send them a reset link.  Probably much like you did.

I would be wanting to chase down why the procedure for password reset is being called twice.

Ron

JohanR

  • Sr. Member
  • ****
  • Posts: 432
    • View Profile
    • Email
Re: How to execute code once of Form Load?
« Reply #5 on: September 25, 2026, 10:56:21 AM »
Hi Ron

Do you have multi TAB support set to true?


Below a snapshot of 2 different servers.
The top section is multi tab = true
The Bottom section is multi tab = false

Hoping to find a definitive way to test for form load/init and to execute code only.
Should work for both situations, multi tab or not.



thanks

Johan

Jane

  • Sr. Member
  • ****
  • Posts: 440
  • Expert on nothing with opinions on everything.
    • View Profile
    • Email
Re: How to execute code once on Form Load?
« Reply #6 on: September 25, 2026, 12:43:53 PM »
Try checking if

Code: [Select]
p_stage=net:web:generate
net:web:generate is an equate for 0

JohanR

  • Sr. Member
  • ****
  • Posts: 432
    • View Profile
    • Email
Re: How to execute code once on Form Load?
« Reply #7 on: September 25, 2026, 09:26:40 PM »
Hi Jane

Thanks for reply.
That is the confusing thing , p_stage is the same in the log

Took the web1 example app and added a memory form with trace in those embeds,
If multi tab support is set to true, then those embeds execute twice and I can't see which flag/stage/event to check for 1st time

I must be missing something,
solution at the moment is to switch off multi tab support,
until I get a definitive check to use.

thanks

Johan 

JohanR

  • Sr. Member
  • ****
  • Posts: 432
    • View Profile
    • Email
Re: How to execute code once on Form Load?
« Reply #8 on: September 26, 2026, 12:49:14 AM »
Hi,

This is what I've found,
but don't have enough knowledge on how to manage or control it, or even to confirm 100%.

If multi TAB true
Page is opened or loaded from the the address bar or via an email link etc...
no tabid part of the GET for first request and this triggers a GET /NewTabID, which then triggers a second
SESSIONID=fCFz0FqUHQBltVRRb5WhZyfrmKd1tE

The same page from a menu item, includes the TABID and does not trigger GET /NewTabID
 SESSIONID=fCFz0FqUHQBltVRRb5WhZyfrmKd1tE; x-TabID=7cf99jil


If multi TAB false, all works fine

Hoping for some ideas here, or point where I go wrong.

One possible way is to query the TABid and knowing whether it's required or not?
But as I mentioned, not sure if that is best way.

thanks

Johan



« Last Edit: September 26, 2026, 12:58:02 AM by JohanR »

rjolda

  • Sr. Member
  • ****
  • Posts: 471
    • View Profile
    • Email
Re: How to execute code once on Form Load?
« Reply #9 on: September 26, 2026, 03:48:05 AM »
Hi Johan,
I think that Multi-tabs being checked ON is one of your problems.
From the Net Talk Development Book Version 4:
Ron


Browsers used to be a single window. Then they acquired tabs, and users could open multiple tabs at
one time, even to the same site. This is sort-of like an MDI child window in a desktop application, but
not quite.
This section described multi-tab support, how to turn it on, and what effects it will have on your
application.
Summary
? Turn on the feature in the web server, performance tab
? Note that Login, and Security information exists across all tabs.

How it Works
Users can, and sometimes do, open your web app in multiple tabs in their browser at the same time.
Because all tabs share a Session cookie, the server is unable to tell when a request comes from one tab,
or from some other tab, and hence activities on one tab can influence the "state" of the app in another
tab. This influence is usually unwelcome and can lead to strange behaviors, or in extreme cases data
corruption. 

NetTalk 8 introduced Multi-Tab support which can be enabled in the WebServer procedure, Settings /
Performance Tab.
If this support is on then each tab is given a unique identifier, and requests from that tab include the
identifier. In this way the app can identify which tab is making a request, and hence keep the state in
one tab different to the state in another tab, so setting a session value in one tab will not set that value
in other tabs (See below for how to create cross-tab values). 
The session itself is still shared. The following states are stored in the session itself, and NOT in the
session data queue. Thus if any of the following states change in one tab they will therefore change in all
tabs.
LoggedIn (yes or no) 
The details of who is logged in are usually stored in the SessionData, and that SessionData will not
change. However the overall state (logged in / not logged in) will be consistent across all the tabs. If the


DEVELOPING WEB APPLICATIONS WITH NETTALK

Page | 101

 

user wishes to Login as 2 different users13 at the same time, in different tabs, then they would need to
login on one tab, open a new tab and login again with different credentials. As the programmer, you
should record who is logged in in SessionData and use the SessionData where applicable. Because each
tab has distinct session data the user will effectively be logged in twice. If they logout in any tab they will
be logged out in all tabs.
If you are logging login's and logouts by embedding code in the SetSessionLoggedIn,
ValidateLogin and/or NotifyDeleteSession then you need to be aware that you'll only get
one call to these methods, but in effect multiple logout's may be occurring at this point.

JohanR

  • Sr. Member
  • ****
  • Posts: 432
    • View Profile
    • Email
Re: How to execute code once on Form Load?
« Reply #10 on: Today at 04:21:44 AM »
Hi,

Yes I think def the multi-tab is at play.

Think will try to make the meeting on Wed,
perhaps a live question and answer will help to show where my problem is, or my implementation is going wrong.

thanks for all your assistance

Johan