Load javascript action and php code

emmexx 26 Jul, 2016
In CF V4 I always used php code inside the js action.
Now I tried to do the same in CF V5 and I got the following error:

Fatal error: Call to undefined method JDocumentHtml::addJsCode() in /mysite/administrator/components/com_chronoforms5/chronoforms/actions/js/js.php on line 38


There's a FAQ that refers to V5 too: https://www.chronoengine.com/faqs/54-cfv4/cfv4-validation/2597-how-can-i-add-javascript-to-my-form.html

My code simply loads a css and a js file from the web:

<?php
$doc =& JFactory::getDocument();
$doc->addStyleSheet('https://npmcdn.com/leaflet@0.7.7/dist/leaflet.css');
$doc->addScript('https://npmcdn.com/leaflet@0.7.7/dist/leaflet.js');
?>

jQuery(document).ready(function()
{
...


Php and js code appear to be incompatible.

Any suggestion?

Thank you
maxx
GreyHead 26 Jul, 2016
Answer
Hi Maxx,

The problem is that Max used $doc for another purpose in CFv5 :-( If you use $jdoc instead your code should be OK - something like this:
<?php
$jdoc = \JFactory::getDocument();
$jdoc->addStyleSheet(JURI::root().'leaflet@0.7.7/dist/leaflet.css');
$jdoc->addScript(JURI::root().'leaflet@0.7.7/dist/leaflet.js');
?>

Bob
emmexx 26 Jul, 2016
Oh, cool. Good to know the news about $doc. 😉

Thank you very much

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