PHP value shared in "Custom Code" and form element

flyeminent 06 Jul, 2011
Hi everyone,
This might be a simple question, but I don't know what are the keywords to search for the solution.
Generally speaking, I would like a form element (i.e. submit button) to access a variable declared in PHP. The scenario is, there is a submit button element in the form, which will be hided/shown by altering the "class" value according to some logic in PHP. The logic is done in "Custom Code" action in "OnLoad" event. In "Custom Code" action (in "OnLoad" event):
<?php 
$class = "";
if($recordFound)
  $class="show";
else
  $class="hide";
?>

The code of "Submit" button in the form:
<input type="submit" class="<?php echo $class; ?>" />

Unfortunately, the "Submit" button cannot get the value of "$class". It is also not working if I move the entire code snippet from "Custom Code" into "Submit" button, since "Submit" button also cannot access "$recordFound". I know JavaScript could be an option. However, I want to avoid using JS as much as possible. Are there any workable solutions without JS?
Thank you in advance for any suggestions!
GreyHead 06 Jul, 2011
Hi flyeminent,

This should work OK but you prbably need to declare $class as global in both places.
<?php
global $class;
$class = "";
if($recordFound) {
  $class="show";
} else {
  $class="hide";
}
?>

<input type="submit" class="<?php global $class; echo $class; ?>" />

Bob
flyeminent 06 Jul, 2011
Ok, I will try that global variable tonight.
Just a follow-up question, if I declare "$class" as global variable, will the variable be visible to other forms (including form element, form actions etc.)?
flyeminent 06 Jul, 2011
Hi GreyHead,
I just tried your suggestion but it was not working. I pasted my code for your investigation. I really appreciate if you could work out this problem without introducing JavaScript.
Above my "Submit Button", I use a "Custom Element" to try to output the variable value from PHP. The code is

<p><?php
global $add_button_class;
echo "button class = ".$add_button_class;
?></p>

In form's "OnLoad" event, I use a "Custom Code" action to compute the logic, the code is

<?php
  global $add_button_class;
  $found= //Get from database
  if($found) {
    $add_button_class = "hide";
  }
  else {
    $add_button_class = "show";
  }
?>

Unfortunately, the custom element displayed "button class = " on the front end, which means it could not read the "$add_button_class" variable even it is declared as global.
GreyHead 08 Jul, 2011
Hi flyeminent ,

Do you have the Custom Code action before the Show HTML action in the OnLoad event?

Bob
flyeminent 09 Jul, 2011
Hello GreyHead,
Yes, the "Custom Code" action is before the "Show html" action. The "Load JS" action after "Custom Code" action can access the global (or non global) php variable, but it seems that "Show html" action is blind of the global php variables.😢
GreyHead 09 Jul, 2011
Hi flyeminent ,

That's weird, I'm not sure how the scope can work like that :-(

Please take a Form Backup using the icon in the Forms Manager and post it here (as a zipped file) or PM or email it to me and I'll take a closer look.

Bob
This topic is locked and no more replies can be posted.