Top Lines

Run a tools file in Concrete5 via Ajax

by Dave Reeder , 16 July 2018

Dave Reeder
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:

<?php
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:

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

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

Ask a Question...

Please complete the form below to send us a message:

Stay Informed.

To stay up to date with special offers, news & tips, please subscribe to our newsletter:

Please note, we never share your data with any third party and you can unsubscribe from emails at any time and request that your data is deleted.