Welcome to the nuBuilder forums!

Please register and login to view forums and other content only available to registered users.

[Added] Form properties / Hash Cookies

Information about updates, news, Code Library
nac
Posts: 110
Joined: Tue Dec 12, 2017 11:28 pm
Location: Aberdeen, UK

[Added] Form properties / Hash Cookies

Unread post by nac »

For some time, nuBuilder has used the JavaScript (JS) function nuSetProperty() to set values (as form properties) that can be used as 'hash cookies' in server-side PHP code. The nuSetProperty() function was subsequently modified with a third parameter which, if set true, would store the property name : value pair in the session record. This meant that the property value would not be 'lost' during form loading operations; it persisted for the duration of the session. The JS function nuGetProperty() is used to retrieve a property value.

A property which is set in JS e.g. nuSetProperty('myname','Bruce Bogtrotter') can be retrieved in PHP with the construct $nm = "#myname#"
This works fine but does have a few drawbacks most notably, if the property (hash cookie) has not been set, the variable takes the value of the hash cookie place holder itself.

To address this, and to provide consistency between JS and PHP code, we have introduced the PHP functions nuSetProperty() and nuGetProperty().
As much as possible, these behave like their JS equivalents, using the same parameters. Consider the following statements:

Code: Select all

$nma = "#myname#" ;   // A
$nmb = nuGetProperty("myname");  //  B
If the property "myname" has been set, then $nma and $nmb will be the same. If it is not set, then $nma will be "#myname#" but $nmb will be null.

Please note the following caveats.
  • If a global property is set in PHP, the value does not become available to JS until a new form is loaded. If you want the property to be available without loading a new form, then nuJavascriptCallback() can be used to set the property explicitly in JS.
  • Be careful with naming. It is good practice to avoid using the same property name for both form (local) and global properties. The issues are analogous to scope-related confusions that can arise in programming languages.
  • In PHP, nuGetProperty() will return null if the property does not exist/has not been set.
The latest version at https://github.com/nuBuilder/nuBuilder-4.5 contains these new functions and we would like to invite users to check their existing applications (on test platforms, of course ) with this updated code to ensure that the behaviour is unaffected. We would also welcome any testing of the new PHP functions.

Please report back any issues (or suggestions) here on the forum.

Many thanks,

Neil
Post Reply