Top Lines

Run a tools file in Concrete5 via Ajax

Dave Reeder

by Dave Reeder , 16 July 2018

Run a tools file in Concrete5 via Ajax
In Concrete5 we can place php files in the tools directory, which we can run with ajax. This means we can load that php file without refreshing the page on page load, or when we click a link or button etc.

First of all we add our tools file under /application/tools.  For testing we can just add a log entry:

defined('C5_EXECUTE') or die("Access Denied.");

Log::AddEntry('Hello World');

Save this as test_tool.php.

Calling our tools file via ajax:

In the page or file where we want to call the tools file, we need to add the path to the tool at the top:

$tools_url = Loader::helper('concrete/urls')->getToolsURL('test_tool');

Note how you don't add .php to the end of the file and you don't put the whole path - Concrete5 knows what to do with just the above.

Then in our file we can add the ajax like so:

            url: "<?php echo $tools_url ?>", 
            context: document.body            
        }).done(function() {
            console.log('ajax complete');

Note, this will trigger the tools file on page load.

All you then need to do is refresh the page and check the logs and you should see your message from your tools file!

Join the Discussion...

Back to Blog

Get Essential News & Offers

Enter your email to receive monthly news and special offers from Made Simple Media:

You can unsubscribe at any time, please read our privacy policy for more information.

Call free on 01403 730080