This topic is locked

Dynamica

2/18/2018 6:08:17 PM
PHPRunner General questions
T
thamestrader author

The title of this post got mis-typed it should be "Dynamically Hide/Show fields on Add page based on a field value"
I have an Add page which contains address fields, the format and content of the first 2 address lines varies accordingly to the type of accommodation; House, Flat, Hotel, Hostel NFA (no fixed abode). In the JSOnload event I have a SELECT CASE on the accommodation type to hide or show the relevant fields and alter the field labels. This is working perfectly.
I've run into an issue by adding duplicate checking in the Before record added event that causes the add page to be redisplayed with error text in $message. I can't find a way to get the jsonload event to hide/show the correct fields. The code below is simplified - in reality I'd need a bit more.
Before Display code: I intended this code to provide the jsonload event whether to hide or show the fields.

// set the session variable if its not defined yet - ie on the first display of the add page only

if (!defined($_SESSION["Hide_addr"]) ){

$_SESSION["Hide_addr"] = true;}

// set proxy variable to HIDE or SHOW - could be true or false but this is easy to detect and debug

if ($_SESSION["Hide_addr"]){

$pageObject->setProxyValue("actype", "HIDE");}

else {

$pageObject->setProxyValue("actype", "SHOW");}
jsonload event code

alert ("Accom " + proxy['actype']);

if (proxy['actype']=="HIDE") {

pageObj.hideField('NumLbl'); pageObj.hideField('NumData');

pageObj.hideField('BldgLbl'); pageObj.hideField('BldgData');

pageObj.hideField('street2'); pageObj.hideField('area');

pageObj.hideField('town'); pageObj.hideField('postcode');}
Before Record added event

// check for NFA and HIDE address fields

if ($values['accom_type'] == "NFA") {

$_SESSION["Hide_addr"] = true;}

else {

$_SESSION["Hide_addr"] = false;}
When the Add page is first displayed the address fields are correctly hidden and they appear dynamically when the Accommodation type is input. But when the add page is redisplayed with the error text the proxy value is always HIDE its never SHOW even though the session variable has been changed.
I've tried a number of other coding solutions which haven't worked but I feel this should work. The alert in the jsonload always displays HIDE so my conclusion is that the proxy values set in the Before record added aren't available to the jsonload and that the before display code is only executed once before the first display of the page.
Does anyone known what the control flow is between events? ie does Before Display always get invoked before the add page is displayed or only before the first display of the Add page.
Thanks in advance.

admin 2/19/2018

Hard to tell what might be wrong by just looking at your code. I can tell though that BeforeDisplay event is called every time Add page (or any other page) is loaded.

T
thamestrader author 2/19/2018

Thanks fro that confirmation Sergey. That enables me to continue trying to use the before display event to provide a proxy variable that the onload event can use to determine the hide/show.
I suspect my very limited knowledge of the javascript environment is not helping. I had previously tried using the document.ready event in onload to carry out the hide/show. On the first display of the Add page its correct but on a redisplay for the error text in $message it doesn't respond to values of the input field that determines the hide/show. Which made me question the use of document.ready.

admin 2/20/2018

You should not be using defined(), it only applies to constants:

http://php.net/manual/en/function.defined.php