JS issue with model variables from DB read

Access database model variables in JavaScript within ChronoForms.

Overview

JavaScript cannot directly access PHP model variables from database reads because they are processed server-side, and square brackets in element IDs cause conflicts.
Use inline PHP within your JavaScript code to echo the required database values directly, making them available to the browser.

Answered
teldrive teldrive 16 Apr, 2014
I apreciate If someone can help , I have detected that when you have fields in your form, created or received from CC list
you can locate in JavaScript without problems

a=$('field1')
b=$('model[field1]');

but when I read DB fields with model, for example "dbdata" i can see these arrays using debugging form tool and use them with PHP but not with JavaScript

PHP(ok)
$a=$form->data['dbdata']['field1'];

JS(fail)
a=$('dbdata[field1]'); always return null(?)😲


Is it posible pass variables from php enviroment to JavaScript enviroment, to use them?
Gr GreyHead 17 Apr, 2014
Hi teldrive,

One of the problems is that [] aren't allowed in input ids (they are OK in names). JavaScript can only access data that is available in the browser and I'm not clear if your data is there. You could add it to hidden inputs in the form or just declare some JS variables in a Custom code action:
<?php
$doc = JFactory::getDocument();
$script = "
var dbdata = {
  'field1': '{$form->data['dbdata']['field1']}',
  'field2': '{$form->data['dbdata']['field2']}'
}
";
$doc->addScriptDeclaration($script);
?>
teldrive teldrive 21 Apr, 2014
Answer
Thanks, I could finally understand the difference between JavaScript that is executed on my browser and php that is executed on hosting

this code didn't work to me i don't know why, anyway I found a workaround

in JavaScript code i can call the "remote" php variables using
a="<?php echo $form->data['dbbase']['field1']; ?>";
This topic is locked and no more replies can be posted.