Written
If you are using code that is repeated on several forms; or writing long or complex code; or you need to include files from a third party API then it can be really useful to include those files into your form instead of trying to work in the ChronoForms textareas.
It's easy to include files into ChronoForms and I do it with almost any code that is more than about twenty lines. Then I can edit it with an FTP enabled editor that has a built in linter for highlighting code errors (I use and recommend Sublime Text 3).
You can include files into textarea boxes in many ChronoForms elements and actions provided that they evaluate PHP when they are run. (The exceptions I can think of at the moment are options boxes of Radio Button and Checkbox groups and the Thank You Message action).
Use the Dynamic Data options with PHP to pre-process the data if needed; and use a Custom Code action in place of the Thank You page action.
I have a macro set up that inserts this code into a textarea. The CFv4 version is:
<?php
include (JPATH_SITE.DS.'components'.DS.'com_chronoforms'.DS.'includes'.DS.$form->form_details->name.DS.'file_name.php');
?>

<?php include (JPATH_SITE.'/components/com_chronoforms5/includes/'.$form->form['Form']['title'].'/load_js.js'); ?>
This will automatically add the form name to the path; replace file_name with the name of your file: I typically use on_load_a.php, on_load_b.php, on_submit_a.php, . . .
For code that is specific to a form I use:
- Custom Code actions to insert PHP that is pre-processing data;
- Load JS actions to insert JavaScript files load_js.js;
- Custom Element elements to insert HTML with PHP that is directly related to the HTML
- Load CSS files to add CSS (though more often I type this directly in the action)
For code that is related to several forms I'll probably put it directly in the 'includes' folder. For example, as a helper file with functions that are used in several files.
For third-party code, like API libraries, I typically create a components/com_chronoforms/extras/ folder and add libraries in sub_folders there.
I keep the included files and folders inside the components/com_chronoforms folder for convenience but you can have them anywhere that is accessible.
You can include files in 'included' files so it is possible to include a custom PHP file that in turn includes a third-party API file. See the Aweber FAQ for an eaxmple - in my version the Custom code is in an included file although the FAQ doesn't say that.
See the screenshot for the structure of my J3 test site - it's easier to see than to describe.
Including JavaScript and CSS
For completeness you can use Joomla! code to load CSS and JavaScript files (or snippets) into the page header.
<?php $doc =& JFactory::getDocument(); $doc->addScript('url_of_js_file.js'); $doc->addScriptDeclaration('some js snippet'); $doc->addStyleSheet('url_of_css_file.css'); $doc->addStyleDeclaration('some css snippet'); // code to force the MooTools library to load // this is sometimes needed to handle the load sequence JHTML::_load('behaviour.mootools'); // Joomla! 2.5 and earlier JHTML::_('behavior.framework'); // Joomla! 3 MooTools core only JHTML::_('behavior.framework', true); // Joomla! 3 MooTools core + more // code load the JQuery library to load (Joomla! 3 only) JHtml::_('jquery.framework'); // load jQuery in no conflict mode // code to load the Bootstrap library (Joomla! 3 only) JHtml::_('bootstrap.framework'); ?>
Please see the Joomla docs for more info on these methods in Joomla! 3 and their limitations.